Ruby on rails 3 Rails使用另一列覆盖更新的\u at以获得正确的缓存\u密钥
我正在使用Rails 3.2.13。 我们使用last_modified_time作为最后更新的列。我的问题是,当我执行Ruby on rails 3 Rails使用另一列覆盖更新的\u at以获得正确的缓存\u密钥,ruby-on-rails-3,caching,Ruby On Rails 3,Caching,我正在使用Rails 3.2.13。 我们使用last_modified_time作为最后更新的列。我的问题是,当我执行model.cache\u key时,它没有考虑:last\u modified\u time列 Rails中的当前(Rails 3.2.13)实现: # Returns a cache key that can be used to identify this record. # # ==== Examples # # Product.new.cache_key
model.cache\u key
时,它没有考虑:last\u modified\u time列
Rails中的当前(Rails 3.2.13)实现:
# Returns a cache key that can be used to identify this record.
#
# ==== Examples
#
# Product.new.cache_key # => "products/new"
# Product.find(5).cache_key # => "products/5" (updated_at not available)
# Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available)
def cache_key
debugger
case
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
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 cache_key
updated_at = self[:updated_at]
if self.last_modified_time && !updated_at
timestamp = self.last_modified_time.utc.to_s(cache_timestamp_format)
"#{super}-#{timestamp}"
end
end
我的问题是:有没有更简单的方法覆盖:updated_at以获得正确的缓存密钥?我刚刚遇到了同样的问题。这并不一定简单,但如果您打算升级到Rails 4,您只需在模型中重写此方法即可:
private
def timestamp_attributes_for_update
super << :last_modifed_time
end
private
def self.timestamp_attributes_for_update
super << "last_modified_time" # must be string
end
private
用于更新的def timestamp_attributes_
super在rails 5中,timestamp\u attributes\u for\u update
是一个类方法,只接受字符串值而不是符号,因此您可以在模型中执行此操作:
private
def timestamp_attributes_for_update
super << :last_modifed_time
end
private
def self.timestamp_attributes_for_update
super << "last_modified_time" # must be string
end
private
用于更新的def self.timestamp_属性
超级的