module ActiveRecord::Integration
def cache_key
Product.find(5).cache_key # => "products/5" (updated_at not available)
Product.new.cache_key # => "products/new"
Returns a cache key that can be used to identify this record.
def cache_key case when new_record? "#{self.class.model_name.cache_key}/new" when timestamp = max_updated_column_timestamp timestamp = timestamp.utc.to_s(cache_timestamp_format) "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" else "#{self.class.model_name.cache_key}/#{id}" end end
def to_param
user = User.find_by(name: 'Phusion')
end
end
name
def to_param # overridden
class User < ActiveRecord::Base
a path using the user's name instead of the user's id:
You can override +to_param+ in your model to make +user_path+ construct
user_path(user) # => "/users/1"
user = User.find_by(name: 'Phusion')
construct a path with the user object's 'id' in it:
resources :users route. Normally, +user_path+ will
For example, suppose that you have a User model, and that you have a
or nil if this record's unsaved.
object. The default implementation returns this record's id as a String,
Returns a String, which Action Pack uses for constructing an URL to this
def to_param # We can't use alias_method here, because method 'id' optimizes itself on the fly. id && id.to_s # Be sure to stringify the id for routes end