Skip to content

Commit ebd7f2a

Browse files
authored
Merge pull request rails#36985 from anmolarora/fix-take-memoization
Clear ActiveRecord object memoized by take
2 parents 04cfbc8 + 2a65310 commit ebd7f2a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

activerecord/lib/active_record/relation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ def reset
641641
@to_sql = @arel = @loaded = @should_eager_load = nil
642642
@records = [].freeze
643643
@offsets = {}
644+
@take = nil
644645
self
645646
end
646647

activerecord/test/cases/relation/where_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ def test_where_with_relation_on_has_one_association
390390
assert_equal author_addresses(:david_address), author_address
391391
end
392392

393-
394393
def test_where_on_association_with_select_relation
395394
essay = Essay.where(author: Author.where(name: "David").select(:name)).take
396395
assert_equal essays(:david_modest_proposal), essay

activerecord/test/cases/relations_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,32 @@ def test_relation_with_private_kernel_method
20502050
assert_equal [accounts(:signals37)], sub_accounts.available
20512051
end
20522052

2053+
def test_where_with_take_memoization
2054+
5.times do |idx|
2055+
Post.create!(title: idx.to_s, body: idx.to_s)
2056+
end
2057+
2058+
posts = Post.all
2059+
first_post = posts.take
2060+
third_post = posts.where(title: "3").take
2061+
2062+
assert_equal "3", third_post.title
2063+
assert_not_equal first_post.object_id, third_post.object_id
2064+
end
2065+
2066+
def test_find_by_with_take_memoization
2067+
5.times do |idx|
2068+
Post.create!(title: idx.to_s, body: idx.to_s)
2069+
end
2070+
2071+
posts = Post.all
2072+
first_post = posts.take
2073+
third_post = posts.find_by(title: "3")
2074+
2075+
assert_equal "3", third_post.title
2076+
assert_not_equal first_post.object_id, third_post.object_id
2077+
end
2078+
20532079
test "#skip_query_cache!" do
20542080
Post.cache do
20552081
assert_queries(1) do

0 commit comments

Comments
 (0)