Don't cache arguments in #find_by if they are an ActiveRecord::Relation

In this commit, find_by doesn't cache arguments
so that find_by with association subquery works correctly.

Fixes #20817
This commit is contained in:
akihiro17 2015-07-21 01:31:48 +09:00
parent df9faf53e9
commit f798cbd2f3
3 changed files with 13 additions and 1 deletions

@ -1,3 +1,9 @@
* Don't cache arguments in #find_by if they are an ActiveRecord::Relation
Fixes #20817
*Hiroaki Izu*
* Allow fixtures files to set the model class in the YAML file itself.
To load the fixtures file `accounts.yml` as the `User` model, use:

@ -177,7 +177,7 @@ def find_by(*args) # :nodoc:
hash = args.first
return super if hash.values.any? { |v|
v.nil? || Array === v || Hash === v
v.nil? || Array === v || Hash === v || Relation === v
}
# We can't cache Post.find_by(author: david) ...yet

@ -265,6 +265,12 @@ def test_find_by_sql_with_sti_on_joined_table
assert_equal [Account], accounts.collect(&:class).uniq
end
def test_find_by_association_subquery
author = authors(:david)
assert_equal author.post, Post.find_by(author: Author.where(id: author))
assert_equal author.post, Post.find_by(author_id: Author.where(id: author))
end
def test_take
assert_equal topics(:first), Topic.take
end