Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure we are not comparing a string with a symbol in HasManyAssociat…

…ion#inverse_updates_counter_cache?. Fixes #2755, where a counter cache could be decremented twice as far as it was supposed to be.
  • Loading branch information...
commit a48e332f7bd21cd87aadc768c75aa3abecbbc38b 1 parent b8d4692
@jonleighton jonleighton authored
View
5 activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*Rails 3.1.1 (unreleased)*
+* Ensure we are not comparing a string with a symbol in HasManyAssociation#inverse_updates_counter_cache?.
+ Fixes GH #2755, where a counter cache could be decremented twice as far as it was supposed to be.
+
+ [Jon Leighton]
+
* Don't send any queries to the database when the foreign key of a belongs_to is nil. Fixes
GH #2828. [Georg Friedrich]
View
2  activerecord/lib/active_record/reflection.rb
@@ -238,7 +238,7 @@ def counter_cache_column
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
View
10 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -17,6 +17,7 @@
require 'models/line_item'
require 'models/car'
require 'models/bulb'
+require 'models/engine'
class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase
class Invoice < ActiveRecord::Base
@@ -808,6 +809,15 @@ def test_clearing_updates_counter_cache
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
View
2  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)
Please sign in to comment.
Something went wrong with that request. Please try again.