我的模型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
的前缀/后缀功能。
谢谢。
答案 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