aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-07-19 08:18:43 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-07-20 00:50:59 +0900
commit0d56480c4b547915f99f3581065e52a48bd98ca1 (patch)
tree327f0a44a421c44340361648877b585c8c977b0d /activerecord
parent1f7f872ac6c8b57af6e0117bde5f6c38d0bae923 (diff)
downloadrails-0d56480c4b547915f99f3581065e52a48bd98ca1.tar.gz
rails-0d56480c4b547915f99f3581065e52a48bd98ca1.tar.bz2
rails-0d56480c4b547915f99f3581065e52a48bd98ca1.zip
Fix `find_by` with range conditions
`StatementCache` doesn't support range conditions. So we need to through the args to `FinderMethods#find_by` if range value is passed.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/core.rb5
-rw-r--r--activerecord/lib/active_record/statement_cache.rb7
-rw-r--r--activerecord/test/cases/finder_test.rb4
3 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 3ea7b644c2..da0331d9c3 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -166,8 +166,7 @@ module ActiveRecord
id = ids.first
- return super if id.kind_of?(Array) ||
- id.is_a?(ActiveRecord::Base)
+ return super if StatementCache.unsupported_value?(id)
key = primary_key
@@ -192,7 +191,7 @@ module ActiveRecord
hash = args.first
return super if !(Hash === hash) || hash.values.any? { |v|
- v.nil? || Array === v || Hash === v || Relation === v || Base === v
+ StatementCache.unsupported_value?(v)
}
# We can't cache Post.find_by(author: david) ...yet
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb
index 1877489e55..ceb7c66766 100644
--- a/activerecord/lib/active_record/statement_cache.rb
+++ b/activerecord/lib/active_record/statement_cache.rb
@@ -106,6 +106,11 @@ module ActiveRecord
klass.find_by_sql(sql, bind_values, preparable: true, &block)
end
- alias :call :execute
+
+ def self.unsupported_value?(value)
+ case value
+ when NilClass, Array, Range, Hash, Relation, Base then true
+ end
+ end
end
end
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index af21cd529f..b860bb4e18 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -1177,6 +1177,10 @@ class FinderTest < ActiveRecord::TestCase
assert_equal posts(:eager_other), Post.find_by("id = ?", posts(:eager_other).id)
end
+ test "find_by with range conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.find_by(id: posts(:eager_other).id...posts(:misc_by_bob).id)
+ end
+
test "find_by returns nil if the record is missing" do
assert_nil Post.find_by("1 = 0")
end