mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix find_by
and where
consistency
The alternative of #26213. Currently `find_by` and `where` with AR object return inconsistent result. This is caused by statement cache does not support AR object. Passing to finder method to fix the issue. Fixes #26210.
This commit is contained in:
parent
127509c071
commit
b334aa0ea4
2 changed files with 8 additions and 3 deletions
|
@ -196,12 +196,12 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_by(*args) # :nodoc:
|
def find_by(*args) # :nodoc:
|
||||||
return super if scope_attributes? || !(Hash === args.first) || reflect_on_all_aggregations.any?
|
return super if scope_attributes? || reflect_on_all_aggregations.any?
|
||||||
|
|
||||||
hash = args.first
|
hash = args.first
|
||||||
|
|
||||||
return super if hash.values.any? { |v|
|
return super if !(Hash === hash) || hash.values.any? { |v|
|
||||||
v.nil? || Array === v || Hash === v || Relation === v
|
v.nil? || Array === v || Hash === v || Relation === v || Base === v
|
||||||
}
|
}
|
||||||
|
|
||||||
# We can't cache Post.find_by(author: david) ...yet
|
# We can't cache Post.find_by(author: david) ...yet
|
||||||
|
|
|
@ -339,6 +339,11 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
|
assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_by_and_where_consistency_with_active_record_instance
|
||||||
|
author = authors(:david)
|
||||||
|
assert_equal Post.where(author_id: author).take, Post.find_by(author_id: author)
|
||||||
|
end
|
||||||
|
|
||||||
def test_take
|
def test_take
|
||||||
assert_equal topics(:first), Topic.take
|
assert_equal topics(:first), Topic.take
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue