diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-06 15:58:07 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-06 15:58:07 +0100 |
commit | b24d668859c5836c0e3ed277b2022a1a39eb3f8e (patch) | |
tree | 9ba6b20c7bb26165953748e4ff9a5305c550575d | |
parent | 9f3e732e65dfa978e036a3b0df3578b2d6a6510a (diff) | |
download | rails-b24d668859c5836c0e3ed277b2022a1a39eb3f8e.tar.gz rails-b24d668859c5836c0e3ed277b2022a1a39eb3f8e.tar.bz2 rails-b24d668859c5836c0e3ed277b2022a1a39eb3f8e.zip |
Ensure we are not comparing a string with a symbol in HasManyAssociation#inverse_updates_counter_cache?. Fixes #2755, where a counter cache could be decremented twice as far as it was supposed to be.
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/models/car.rb | 2 |
3 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index a2324039cf..89179779e3 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -226,7 +226,7 @@ module ActiveRecord if options[:counter_cache] == true "#{active_record.name.demodulize.underscore.pluralize}_count" elsif options[:counter_cache] - options[:counter_cache] + options[:counter_cache].to_s end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a2764f3e3b..1e59931963 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -17,6 +17,7 @@ require 'models/invoice' require 'models/line_item' require 'models/car' require 'models/bulb' +require 'models/engine' class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase class Invoice < ActiveRecord::Base @@ -850,6 +851,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_clearing_updates_counter_cache_when_inverse_counter_cache_is_a_symbol_with_dependent_destroy + car = Car.first + car.engines.create! + + assert_difference 'car.reload.engines_count', -1 do + car.engines.clear + end + end + def test_clearing_a_dependent_association_collection firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index 76f20b1061..b9c2e8ec9a 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -8,7 +8,7 @@ class Car < ActiveRecord::Base has_one :frickinawesome_bulb, :class_name => "Bulb", :conditions => { :frickinawesome => true } has_many :tyres - has_many :engines + has_many :engines, :dependent => :destroy has_many :wheels, :as => :wheelable scope :incl_tyres, includes(:tyres) |