我发现保存记录后清除了查询缓存。有谁知道这是什么问题?请参阅以下代码。
ActiveRecord::Base.cache do
policy_file = FormUnit.find_by_number("F.XXX")
# @query_cache has a value that is a query of policy_file
pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")
# fetched it from cache
FormUnit.find_by_number("F.XXX")
policy_file_branch = policy_file.forms.build(edition: "10/10", effective_date: "2013/1/1")
# still can fetch it from cache query.
pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")
FormUnit.find_by_number("F.XXX")
policy_file_branch.save
# now, @query_cache was empty and had to fetch it in sql query.
pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")
FormUnit.find_by_number("F.XXX")
end