- C
- T
类公共方法
cache_timestamp_format 链接
指示用于生成缓存键中时间戳的格式(如果版本控制已关闭)。可接受 Time::DATE_FORMATS 中的任何符号。
默认情况下,这是 :usec。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 16 class_attribute :cache_timestamp_format, instance_writer: false, default: :usec
cache_versioning 链接
指示是否使用稳定的 cache_key 方法,该方法附带 cache_version 方法中不断变化的 cache_version。
从 Rails 5.2 及更高版本开始,默认值为 true。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 24 class_attribute :cache_versioning, instance_writer: false, default: false
collection_cache_versioning 链接
指示是否为集合使用稳定的 cache_key 方法,该方法附带 cache_version 方法中不断变化的 cache_version。
在 Rails 6.1 之前,默认值为 false。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 32 class_attribute :collection_cache_versioning, instance_writer: false, default: false
实例公共方法
cache_key() 链接
返回一个稳定的缓存键,可用于标识此记录。
Product.new.cache_key # => "products/new" Product.find(5).cache_key # => "products/5"
如果 ActiveRecord::Base.cache_versioning 已关闭(例如在 Rails 5.1 及更早版本中),则缓存键还将包含一个版本。
Product.cache_versioning = false Product.find(5).cache_key # => "products/5-20071224150000" (updated_at available)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 72 def cache_key if new_record? "#{model_name.cache_key}/new" else if cache_version "#{model_name.cache_key}/#{id}" else timestamp = max_updated_column_timestamp if timestamp timestamp = timestamp.utc.to_fs(cache_timestamp_format) "#{model_name.cache_key}/#{id}-#{timestamp}" else "#{model_name.cache_key}/#{id}" end end end end
cache_key_with_version() 链接
返回一个缓存键以及版本。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 114 def cache_key_with_version if version = cache_version "#{cache_key}-#{version}" else cache_key end end
cache_version() 链接
返回一个缓存版本,可与缓存键一起用于构成可回收的缓存方案。默认情况下,`updated_at` 列用于 cache_version,但此方法可以被覆盖以返回其他内容。
注意,如果 ActiveRecord::Base.cache_versioning 设置为 false,此方法将返回 nil。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 97 def cache_version return unless cache_versioning if has_attribute?("updated_at") timestamp = updated_at_before_type_cast if can_use_fast_cache_version?(timestamp) raw_timestamp_to_cache_version(timestamp) elsif timestamp = updated_at timestamp.utc.to_fs(cache_timestamp_format) end elsif self.class.has_attribute?("updated_at") raise ActiveModel::MissingAttributeError, "missing attribute 'updated_at' for #{self.class}" end end
to_param() 链接
返回一个 String,Action Pack 使用它来构造此对象的 URL。默认实现将此记录的 id 作为 String 返回,如果此记录未保存,则返回 nil。
例如,假设您有一个 User 模型,并且您有一个 resources :users 路由。通常,user_path 将构造一个包含用户对象的“id”的路径。
user = User.find_by(name: 'Phusion') user_path(user) # => "/users/1"
您可以在您的模型中重写 to_param,以便 user_path 使用用户的名称而不是用户的 id 来构造路径。
class User < ActiveRecord::Base def to_param # overridden name end end user = User.find_by(name: 'Phusion') user_path(user) # => "/users/Phusion"
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/integration.rb, line 57 def to_param return unless id Array(id).join(self.class.param_delimiter) end