This project is archived and is in readonly mode.
reset_counters doesn't work with polymorphic associations
Reported by klew | June 26th, 2010 @ 07:05 PM | in 3.0.2
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.
Comments and changes to this ticket
-
Neeraj Singh June 27th, 2010 @ 03:51 AM
- 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.
-
Neeraj Singh June 27th, 2010 @ 04:12 AM
- Tag changed from nested attributes, polymorphic, reset_counters to nested attributes, polymorphic, rails3, reset_counters
-
Santiago Pastorino June 27th, 2010 @ 04:23 AM
- Milestone cleared.
- Assigned user set to José Valim
-
José Valim June 27th, 2010 @ 08:07 AM
Neeraj, what do we need in order to have a complete fix? What is blocking it? Thanks! :)
-
Neeraj Singh June 27th, 2010 @ 01:24 PM
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.
-
Neeraj Singh June 27th, 2010 @ 04:42 PM
- Tag changed from nested attributes, polymorphic, rails3, reset_counters to nested attributes, patch, polymorphic, rails3, reset_counters
Attached is patch with test.
-
Repository July 8th, 2010 @ 10:45 PM
- State changed from open to resolved
(from [1e53404fe9c39ad0849894d73e431315be8c0bf0]) reset_counter should work with non-traditional belongs_to and polymorphic belongs_to
[#4984 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/1e53404fe9c39ad0849894d73e4313...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 4984 reset_counters doesn't work with polymorphic associations [#4984 state:resolved]