From 2a653108900c7e35b6d93148097a2f862b8a9f0b Mon Sep 17 00:00:00 2001 From: Anmol Arora Date: Tue, 20 Aug 2019 00:33:03 +0530 Subject: [PATCH] Clear ActiveRecord object memoized by take --- activerecord/lib/active_record/relation.rb | 1 + .../test/cases/relation/where_test.rb | 1 - activerecord/test/cases/relations_test.rb | 26 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index dc044c3da3..793cd90e67 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -641,6 +641,7 @@ def reset @to_sql = @arel = @loaded = @should_eager_load = nil @records = [].freeze @offsets = {} + @take = nil self end diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index aad30ddea0..417a50b3f6 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -390,7 +390,6 @@ def test_where_with_relation_on_has_one_association assert_equal author_addresses(:david_address), author_address end - def test_where_on_association_with_select_relation essay = Essay.where(author: Author.where(name: "David").select(:name)).take assert_equal essays(:david_modest_proposal), essay diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index cf2da90326..77f954b4d2 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -2050,6 +2050,32 @@ def test_relation_with_private_kernel_method assert_equal [accounts(:signals37)], sub_accounts.available end + def test_where_with_take_memoization + 5.times do |idx| + Post.create!(title: idx.to_s, body: idx.to_s) + end + + posts = Post.all + first_post = posts.take + third_post = posts.where(title: "3").take + + assert_equal "3", third_post.title + assert_not_equal first_post.object_id, third_post.object_id + end + + def test_find_by_with_take_memoization + 5.times do |idx| + Post.create!(title: idx.to_s, body: idx.to_s) + end + + posts = Post.all + first_post = posts.take + third_post = posts.find_by(title: "3") + + assert_equal "3", third_post.title + assert_not_equal first_post.object_id, third_post.object_id + end + test "#skip_query_cache!" do Post.cache do assert_queries(1) do