diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-04-27 22:01:09 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-27 22:01:09 +0900 |
commit | 0a8fe15ba8fc7c36253996c557a0012f718d7d60 (patch) | |
tree | c67061f7bc8e6124649c7f8d6b2556f60d61b20b /activerecord/test | |
parent | 46b46e85c72f78e869a1b0038040849911c7e2c5 (diff) | |
parent | b07b97057fba21cbccca9a81630173a88406175a (diff) | |
download | rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.tar.gz rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.tar.bz2 rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.zip |
Merge pull request #32617 from tgturner/size-should-use-available-association
Loaded associations should not run a new query when size is called
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 74 |
1 files changed, 74 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 33fe5ccabc..feb7390e2c 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -993,6 +993,16 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, post.readers.size end + def test_collection_empty_with_dirty_target + post = posts(:thinking) + assert_equal [], post.reader_ids + assert_empty post.readers + post.readers.reset + post.readers.build + assert_equal [], post.reader_ids + assert_not_empty post.readers + end + def test_collection_size_twice_for_regressions post = posts(:thinking) assert_equal 0, post.readers.size @@ -2573,6 +2583,70 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + test "calling size on an association that has not been loaded performs a query" do + car = Car.create! + Bulb.create(car_id: car.id) + + car_two = Car.create! + + assert_queries(1) do + assert_equal 1, car.bulbs.size + end + + assert_queries(1) do + assert_equal 0, car_two.bulbs.size + end + end + + test "calling size on an association that has been loaded does not perform query" do + car = Car.create! + Bulb.create(car_id: car.id) + car.bulb_ids + + car_two = Car.create! + car_two.bulb_ids + + assert_no_queries do + assert_equal 1, car.bulbs.size + end + + assert_no_queries do + assert_equal 0, car_two.bulbs.size + end + end + + test "calling empty on an association that has not been loaded performs a query" do + car = Car.create! + Bulb.create(car_id: car.id) + + car_two = Car.create! + + assert_queries(1) do + assert_not_empty car.bulbs + end + + assert_queries(1) do + assert_empty car_two.bulbs + end + end + + test "calling empty on an association that has been loaded does not performs query" do + car = Car.create! + Bulb.create(car_id: car.id) + car.bulb_ids + + car_two = Car.create! + car_two.bulb_ids + + assert_no_queries do + assert_not_empty car.bulbs + end + + assert_no_queries do + assert_empty car_two.bulbs + end + end + class AuthorWithErrorDestroyingAssociation < ActiveRecord::Base self.table_name = "authors" has_many :posts_with_error_destroying, |