跳到內容 跳到搜尋

Active Record Store

Store 為您提供一個方便的包裝,可用於將雜湊儲存在單一欄位中序列化。這就像是一個簡單的鍵/值儲存,內建在您的紀錄中,讓您在單一紀錄的上下文中不需要對該儲存進行查詢。

然後,您可以宣告存取器來存取此儲存,這些存取器可以像模型的其他任何屬性一樣進行存取。這有助於將儲存鍵輕易地公開至表單或其他已內建於只存取模型屬性的位置。

每個存取器附帶骯髒追蹤方法(key_changed?key_waskey_change)及方法來存取上次儲存時所做的變更(saved_change_to_key?saved_change_to_keykey_before_last_save)。

注意:對於存取器沒有 key_will_change! 方法,請改用 store_will_change!

請確保將用於序列化儲存的資料庫欄宣告為文字類型,以提供足夠的空間。

您可以設定自訂編碼器來將序列化的屬性編碼/解碼為不同格式。JSON、YAML、Marshal 預設為支援。通常它可以是提供 loaddump 的任何包裝器。

注意:如果您使用結構化資料庫資料類型(例如 PostgreSQL hstore/json、MySQL 5.7+ json 或 SQLite 3.38+ json),則不需要由 .store 所提供的序列化。只需改用 .store_accessor 來產生存取器方法即可。請注意,這些欄位使用字串作為鍵雜湊,不允許使用符號進行存取。

注意:預設驗證,uniqueness 除外,將會正常運作。例如,如果您想針對 hstore 檢查 uniqueness,您必須使用自訂驗證來處理。

範例

class User < ActiveRecord::Base
  store :settings, accessors: [ :color, :homepage ], coder: JSON
  store :parent, accessors: [ :name ], coder: JSON, prefix: true
  store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
  store :settings, accessors: [ :two_factor_auth ], suffix: true
  store :settings, accessors: [ :login_retry ], suffix: :config
end

u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
u.color                          # Accessor stored attribute
u.parent_name                    # Accessor stored attribute with prefix
u.partner_name                   # Accessor stored attribute with custom prefix
u.two_factor_auth_settings       # Accessor stored attribute with suffix
u.login_retry_config             # Accessor stored attribute with custom suffix
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor

# There is no difference between strings and symbols for accessing custom attributes
u.settings[:country]  # => 'Denmark'
u.settings['country'] # => 'Denmark'

# Dirty tracking
u.color = 'green'
u.color_changed? # => true
u.color_was # => 'black'
u.color_change # => ['black', 'green']

# Add additional accessors to an existing store through store_accessor
class SuperUser < User
  store_accessor :settings, :privileges, :servants
  store_accessor :parent, :birthday, prefix: true
  store_accessor :settings, :secret_question, suffix: :config
end

可以使用 .stored_attributes 擷取儲存的屬性名稱。

User.stored_attributes[:settings] # => [:color, :homepage, :two_factor_auth, :login_retry]

覆寫預設存取器

所有儲存的值都會自動透過 Active Record 物件的存取器取得,但有時您需要為這項行為制定特殊化。這可藉由覆寫預設存取器(使用與屬性名稱相同的名稱)及呼叫 super 來實際變更內容。

class Song < ActiveRecord::Base
  # Uses a stored integer to hold the volume adjustment of the song
  store :settings, accessors: [:volume_adjustment]

  def volume_adjustment=(decibels)
    super(decibels.to_i)
  end

  def volume_adjustment
    super.to_i
  end
end
命名空間
方法
R
W

屬性

[RW] local_stored_attributes

執行個體的私人方法

read_store_attribute(store_attribute, key)

# File activerecord/lib/active_record/store.rb, line 209
def read_store_attribute(store_attribute, key) # :doc:
  accessor = store_accessor_for(store_attribute)
  accessor.read(self, store_attribute, key)
end

write_store_attribute(store_attribute, key, value)

# File activerecord/lib/active_record/store.rb, line 214
def write_store_attribute(store_attribute, key, value) # :doc:
  accessor = store_accessor_for(store_attribute)
  accessor.write(self, store_attribute, key, value)
end