跳到內文 跳到搜尋
命名空間
方法
C
T

類別公開方法

cache_timestamp_format

當版本控管關閉時,指定用於產生快取金鑰中時間戳記的格式。接受 `Time::DATE_FORMATS` 中的任何符號。

預設為 `:usec`。

# 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 方法中變更的版本。

預設為 `true`(在 Rails 5.2 及以上版本中)。

# 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 方法中變更的版本。

預設為 `false`(在 Rails 6.1 之前版本中)。

# 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)
# 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()

傳回快取金鑰以及版本。

# 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`。

# 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"
# File activerecord/lib/active_record/integration.rb, line 57
def to_param
  return unless id
  Array(id).join(self.class.param_delimiter)
end