Rails 3.2 で登場した ActiveRecord::Store を利用できます。それはあなたが探しているものを正確に提供します:
- データベース内の 1 つのテキスト列にシリアル化するストア
- 必要に応じて属性を内部に入れます
- さらに、共通属性のアクセサーを定義できます
例:
class User < ActiveRecord::Base
store :settings, accessors: [ :color, :homepage ], coder: JSON
end
u = User.new(color: 'black', homepage: '37signals.com')
u.color # Accessor stored attribute
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'
# Add additional accessors to an existing store through store_accessor
class SuperUser < User
store_accessor :settings, :privileges, :servants
end
貼り付け/詳細:
http://api.rubyonrails.org/classes/ActiveRecord/Store.html