Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

reset_counters doesn't work with polymorphic associations

#4984

If I have models:

class Phone < ActiveRecord::Base
  belongs_to :phonable, :polymorphic => true
end

class Person < ActiveRecord::Base
  has_many :phones, :as => :phonable
end

Trying to execute:

Person.reset_counters 1, :phones
Gives:
NoMethodError: undefined method counter_cache_column for nil:NilClass

Problem is in this line in reset_counters method:

counter_name = child_class.reflect_on_association(self.name.downcase.to_sym).counter_cache_column

Because self.name.downcase.to_sym is not always name of belongs_to association.

Reported by klew · June 26th, 2010 @ 07:05 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • State changed from new to open
    • Importance changed from to Low

    I am attaching a patch with test which does not solve the original problem. However it is a step in that direction.

    Currently following works

    class Car < ActiveRecord::Base
      has_many :brakes
    end
    class Brake < ActiveRecord::Base
      belongs_to :car, :counter_cache => :count_of_items
    end
    Car.reset_counters 1, :brakes
    

    However following does not work

    class Car < ActiveRecord::Base
      has_many :brakes
    end
    class Brake < ActiveRecord::Base
      belongs_to :my_car, :class_name => 'Car', :foreign_key => 'car_id',  :counter_cache => :count_of_items
    end
    Car.reset_counters 1, :brakes
    

    Attached patch fixes this problem.

    June 27th, 2010 @ 03:51 AM

  2. Neeraj Singh
    Neeraj Singh
    • Tag changed from nested attributes, polymorphic, reset_counters to nested attributes, polymorphic, rails3, reset_counters

    June 27th, 2010 @ 04:12 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user set to José Valim

    June 27th, 2010 @ 04:23 AM

  4. José Valim
    José Valim

    Neeraj, what do we need in order to have a complete fix? What is blocking it? Thanks! :)

    June 27th, 2010 @ 08:07 AM

  5. Neeraj Singh
    Neeraj Singh

    Right now I am not able to articulate what was blocking me. I will take a look at ticket again today and will update accordingly.

    June 27th, 2010 @ 01:24 PM

  6. Neeraj Singh
    Neeraj Singh
    • Tag changed from nested attributes, polymorphic, rails3, reset_counters to nested attributes, patch, polymorphic, rails3, reset_counters

    Attached is patch with test.

    June 27th, 2010 @ 04:42 PM

  7. Repository
    Repository
    • State changed from open to resolved

    (from [1e53404fe9c39ad0849894d73e431315be8c0bf0]) reset_counter should work with non-traditional belongs_to and polymorphic belongs_to

    [#4984 reset_counters doesn't work with polymorphic associations state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/1e53404fe9c39ad0849894d73e4313...

    July 8th, 2010 @ 10:45 PM

  8. Jeremy Kemper