mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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:
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 @@ module ActiveRecord
|
|||
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 @@ class FinderTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue