Created
July 23, 2013 14:47
-
-
Save aaronjensen/6062912 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AllCacheKey | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def cache_key | |
pluck("COUNT(*)", "MAX(updated_at)").flatten.map(&:to_i).join("-") | |
end | |
end | |
end |
This is similar to Caching with Rails guide cache_key_for_products
method, isn't it ?
Or i'ts more optimized ?
For Rails 3 you need a little bit more code:
def cache_key
sql = select('COUNT(*), MAX(updated_at)').to_sql
connection.execute(sql).to_a.flatten.map(&:to_i).join('-')
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the multi-valued pluck is a Rails 4 feature, Rails 3 will need something slightly different