aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/belongs_to_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/belongs_to_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/belongs_to_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index 47fd7345c8..c59dc00071 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -19,6 +19,9 @@ require 'models/invoice'
require 'models/line_item'
require 'models/column'
require 'models/record'
+require 'models/ship'
+require 'models/treasure'
+require 'models/parrot'
class BelongsToAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :topics,
@@ -311,6 +314,22 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 1, Company.all.merge!(:includes => :firm_with_select ).find(2).firm_with_select.attributes.size
end
+ def test_belongs_to_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')
+
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed unless counter_cache is given on the relation" do
+ treasure = Treasure.new(name: 'Gold', ship: ship)
+ treasure.save
+ end
+
+ assert_no_difference lambda { ship.reload.treasures_count }, "treasures_count should not be changed unless counter_cache is given on the relation" do
+ treasure = ship.treasures.first
+ treasure.destroy
+ end
+ end
+
def test_belongs_to_counter
debate = Topic.create("title" => "debate")
assert_equal 0, debate.read_attribute("replies_count"), "No replies yet"