aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-09-20 17:53:49 +0900
committerGodfrey Chan <godfreykfc@gmail.com>2014-09-20 17:53:49 +0900
commit90f99689121487328f022fd09841b62378423afe (patch)
tree68ac303c4c4ce352eff16f30727b59a6d6bfece0 /activerecord
parent793e4aa9b63a066d0bf0dbd291969ec9cd2fc204 (diff)
downloadrails-90f99689121487328f022fd09841b62378423afe.tar.gz
rails-90f99689121487328f022fd09841b62378423afe.tar.bz2
rails-90f99689121487328f022fd09841b62378423afe.zip
Fix find_by with associations not working with adequate record
For now, we will just skip the cache when a non-column key is used in the hash. If the future, we can probably move some of the logic in PredicateBuilder.expand up the chain to make caching possible for association queries. Closes #16903 Fixes #16884
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/core.rb3
-rw-r--r--activerecord/test/cases/finder_test.rb5
2 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 68e89b981e..069aa977bf 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -162,6 +162,9 @@ module ActiveRecord
v.nil? || Array === v || Hash === v
}
+ # We can't cache Post.find_by(author: david) ...yet
+ return super unless hash.keys.all? { |k| columns_hash.has_key?(k.to_s) }
+
key = hash.keys
klass = self
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 7228a75548..ac570ecbe0 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -1073,6 +1073,11 @@ class FinderTest < ActiveRecord::TestCase
assert_equal nil, Post.find_by("1 = 0")
end
+ test "find_by with associations" do
+ assert_equal authors(:david), Post.find_by(author: authors(:david)).author
+ assert_equal authors(:mary) , Post.find_by(author: authors(:mary) ).author
+ end
+
test "find_by doesn't have implicit ordering" do
assert_sql(/^((?!ORDER).)*$/) { Post.find_by(id: posts(:eager_other).id) }
end