具有相同键的多个store_accessors

时间:2017-11-14 09:35:23

标签: ruby-on-rails ruby

我的模型Profile包含hstore字段paypal_credentials

class Profile < ActiveRecord::Base
  ...
  store_accessor :paypal_credentials, :preapproval_key, :preapproval_url
  ...
end

现在我需要添加名为customer_paypal_credentials的第二个hstore字段,其中包含相同的键。如何以这种方式使用store_accessor?我找不到store_accessor的前缀/后缀功能。

谢谢。

1 个答案:

答案 0 :(得分:0)

这在Rails 6及更高版本中是可能的:https://api.rubyonrails.org/classes/ActiveRecord/Store.html

来自文档:

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