aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_associations_test.rb
diff options
context:
space:
mode:
authorTristan Gamilis <tristan@2suggestions.com.au>2015-04-09 14:53:15 +1000
committerTristan Gamilis <tristan@2suggestions.com.au>2015-04-09 14:53:34 +1000
commit543523045112c5f5920c486e6fcf2d7e1ffedf5a (patch)
treedbf9081e9b0a8739b5eed5daea9af1ddefc5a19c /activerecord/test/cases/associations/has_many_associations_test.rb
parente0cb21f5f767606ad3ecf2db33855d27aa9f083d (diff)
downloadrails-543523045112c5f5920c486e6fcf2d7e1ffedf5a.tar.gz
rails-543523045112c5f5920c486e6fcf2d7e1ffedf5a.tar.bz2
rails-543523045112c5f5920c486e6fcf2d7e1ffedf5a.zip
Add tests for associations without counter_cache
Assert that counter_cache behaviour is not used on belongs_to or has_many associations if the option is not given explicitly.
Diffstat (limited to 'activerecord/test/cases/associations/has_many_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 171cfbde44..328ea1061f 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -31,6 +31,8 @@ require 'models/student'
require 'models/pirate'
require 'models/ship'
require 'models/ship_part'
+require 'models/treasure'
+require 'models/parrot'
require 'models/tyre'
require 'models/subscriber'
require 'models/subscription'
@@ -932,6 +934,25 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 0, new_firm.clients_of_firm.size
end
+ def test_has_many_without_counter_cache_option
+ # Ship has a conventionally named `treasures_count` column, but the counter_cache
+ # option is not given on the association.
+ ship = Ship.create(name: 'Countless', treasures_count: 10)
+
+ assert_not ship.treasures.instance_variable_get('@association').send(:has_cached_counter?)
+
+ # Count should come from sql count() of treasures rather than treasures_count attribute
+ assert_equal ship.treasures.size, 0
+
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
+ ship.treasures.create(name: 'Gold')
+ end
+
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed" do
+ ship.treasures.destroy_all
+ end
+ end
+
def test_deleting_updates_counter_cache
topic = Topic.order("id ASC").first
assert_equal topic.replies.to_a.size, topic.replies_count