Method: Mongoid::Document#cache_key

Defined in:
lib/mongoid/document.rb

#cache_keyString

Print out the cache key. This will append different values on the plural model name.

If new_record? - will append /new If not - will append /id-updated_at.to_s(:number) Without updated_at - will append /id

This is usually called insode a cache() block

Examples:

Returns the cache key

document.cache_key

Returns:

  • (String)

    the string with or without updated_at

Since:

  • 2.4.0



229
230
231
232
233
# File 'lib/mongoid/document.rb', line 229

def cache_key
  return "#{model_key}/new" if new_record?
  return "#{model_key}/#{id}-#{updated_at.utc.to_s(:number)}" unless self[:updated_at].nil?
  "#{model_key}/#{id}"
end