1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Enable SQL statement cache for find on base class as with find_by

Related and follows d333d85254.
This commit is contained in:
Ryuta Kamizono 2019-02-26 04:45:13 +09:00
parent 357aa83f84
commit dda5501e80
2 changed files with 23 additions and 4 deletions

View file

@ -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

View file

@ -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