aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-26 04:45:13 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-03-01 16:28:32 +0900
commitdda5501e80cb98bbe119ac2f9fcf9b7049e121ce (patch)
treed56d3c35cd554729654a64a5ee692fa1681e88c3 /activerecord
parent357aa83f84695a31328b17be4080f8ab4993fcb8 (diff)
downloadrails-dda5501e80cb98bbe119ac2f9fcf9b7049e121ce.tar.gz
rails-dda5501e80cb98bbe119ac2f9fcf9b7049e121ce.tar.bz2
rails-dda5501e80cb98bbe119ac2f9fcf9b7049e121ce.zip
Enable SQL statement cache for `find` on base class as with `find_by`
Related and follows d333d85254d27cd572e6ecce8ee850c107a4f340.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/test/cases/bind_parameter_test.rb25
2 files changed, 23 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 18cfac1f2f..eb4b48bc37 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -161,7 +161,7 @@ module ActiveRecord
return super if block_given? ||
primary_key.nil? ||
scope_attributes? ||
- columns_hash.include?(inheritance_column)
+ columns_hash.key?(inheritance_column) && !base_class?
id = ids.first
diff --git a/activerecord/test/cases/bind_parameter_test.rb b/activerecord/test/cases/bind_parameter_test.rb
index b89f054821..03cc0dffbc 100644
--- a/activerecord/test/cases/bind_parameter_test.rb
+++ b/activerecord/test/cases/bind_parameter_test.rb
@@ -54,17 +54,36 @@ if ActiveRecord::Base.connection.prepared_statements
@connection.disable_query_cache!
end
+ def test_statement_cache_with_find
+ @connection.clear_cache!
+
+ assert_equal 1, Topic.find(1).id
+ assert_raises(RecordNotFound) { SillyReply.find(2) }
+
+ topic_sql = cached_statement(Topic, Topic.primary_key)
+ assert_includes statement_cache, to_sql_key(topic_sql)
+
+ e = assert_raise { cached_statement(SillyReply, SillyReply.primary_key) }
+ assert_equal "SillyReply has no cached statement by \"id\"", e.message
+
+ replies = SillyReply.where(id: 2).limit(1)
+ assert_includes statement_cache, to_sql_key(replies.arel)
+ end
+
def test_statement_cache_with_find_by
@connection.clear_cache!
assert_equal 1, Topic.find_by!(id: 1).id
- assert_equal 2, Reply.find_by!(id: 2).id
+ assert_raises(RecordNotFound) { SillyReply.find_by!(id: 2) }
topic_sql = cached_statement(Topic, [:id])
assert_includes statement_cache, to_sql_key(topic_sql)
- e = assert_raise { cached_statement(Reply, [:id]) }
- assert_equal "Reply has no cached statement by [:id]", e.message
+ e = assert_raise { cached_statement(SillyReply, [:id]) }
+ assert_equal "SillyReply has no cached statement by [:id]", e.message
+
+ replies = SillyReply.where(id: 2).limit(1)
+ assert_includes statement_cache, to_sql_key(replies.arel)
end
def test_statement_cache_with_in_clause