1
0
Fork 0
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:
Ryuta Kamizono 2017-01-04 12:19:41 +09:00
parent 127509c071
commit b334aa0ea4
2 changed files with 8 additions and 3 deletions

View file

@ -196,12 +196,12 @@ module ActiveRecord
end
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
return super if hash.values.any? { |v|
v.nil? || Array === v || Hash === v || Relation === v
return super if !(Hash === hash) || hash.values.any? { |v|
v.nil? || Array === v || Hash === v || Relation === v || Base === v
}
# We can't cache Post.find_by(author: david) ...yet

View file

@ -339,6 +339,11 @@ class FinderTest < ActiveRecord::TestCase
assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
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
assert_equal topics(:first), Topic.take
end