From b334aa0ea4008221c475adc5b124f3b23b785aba Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 4 Jan 2017 12:19:41 +0900 Subject: 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. --- activerecord/lib/active_record/core.rb | 6 +++--- activerecord/test/cases/finder_test.rb | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 5d0f82130d..6d2361c4ac 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -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 diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 97010b1fd9..e0ad9f5ec1 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -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 -- cgit v1.2.3