module Apartment::Model::ClassMethods
def cached_find_by_statement(key, &block)
while if we run:
`Setting.find_by(key: 'something', value: 'amazing')` key will have an array of symbols: `[:key, :something]`
E.g. If we run the following query:
NOTE: key can either be an array of symbols or a single value.
def cached_find_by_statement(key, &block) # Modifying the cache key to have a reference to the current tenant, # so the cached statement is referring only to the tenant in which we've # executed this cache_key = if key.is_a? String "#{Apartment::Tenant.current}_#{key}" else # NOTE: In Rails 6.0.4 we start receiving an ActiveRecord::Reflection::BelongsToReflection # as the key, which wouldn't work well with an array. [Apartment::Tenant.current] + Array.wrap(key) end cache = @find_by_statement_cache[connection.prepared_statements] cache.compute_if_absent(cache_key) { ActiveRecord::StatementCache.create(connection, &block) } end