diff options
author | Stefan Kanev <stefan.kanev@gmail.com> | 2014-08-09 22:19:02 +0300 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-07-19 15:52:29 -0600 |
commit | 0ed096ddf5416fefa3afacb72c64632c02826f95 (patch) | |
tree | cc715ac6cf6b362738eb219068612ed12577f4ba /activerecord/test/cases | |
parent | 0bd247cc7679adb3c2e30b46ecc9afd57ac46c45 (diff) | |
download | rails-0ed096ddf5416fefa3afacb72c64632c02826f95.tar.gz rails-0ed096ddf5416fefa3afacb72c64632c02826f95.tar.bz2 rails-0ed096ddf5416fefa3afacb72c64632c02826f95.zip |
Fix counter_cache for polymorphic associations
Also removes a false positive test that depends on the fixed bug:
At this time, counter_cache does not work with polymorphic relationships
(which is a bug). The test was added to make sure that no
StaleObjectError is raised when the car is destroyed. No such error is
currently raised because the lock version is not incremented by
appending a wheel to the car.
Furthermore, `assert_difference` succeeds because `car.wheels.count`
does not check the counter cache, but the collection size. The test will
fail if it is replaced with `car.wheels_count || 0`.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/counter_cache_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/locking_test.rb | 12 |
2 files changed, 13 insertions, 12 deletions
diff --git a/activerecord/test/cases/counter_cache_test.rb b/activerecord/test/cases/counter_cache_test.rb index 1f5055b2a2..922cb59280 100644 --- a/activerecord/test/cases/counter_cache_test.rb +++ b/activerecord/test/cases/counter_cache_test.rb @@ -1,6 +1,7 @@ require 'cases/helper' require 'models/topic' require 'models/car' +require 'models/aircraft' require 'models/wheel' require 'models/engine' require 'models/reply' @@ -198,4 +199,16 @@ class CounterCacheTest < ActiveRecord::TestCase assert_equal 2, car.engines_count assert_equal 2, car.reload.engines_count end + + test "update counters in a polymorphic relationship" do + aircraft = Aircraft.create! + + assert_difference 'aircraft.reload.wheels_count' do + aircraft.wheels << Wheel.create! + end + + assert_difference 'aircraft.reload.wheels_count', -1 do + aircraft.wheels.first.destroy + end + end end diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index dbdcc84b7d..839bbea5bb 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -263,18 +263,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase end end - def test_polymorphic_destroy_with_dependencies_and_lock_version - car = Car.create! - - assert_difference 'car.wheels.count' do - car.wheels << Wheel.create! - end - assert_difference 'car.wheels.count', -1 do - car.destroy - end - assert car.destroyed? - end - def test_removing_has_and_belongs_to_many_associations_upon_destroy p = RichPerson.create! first_name: 'Jon' p.treasures.create! |