我有带hstore字段的模型:

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

现在我需要用相同的键添加第二个名为Profile的hstore字段。我怎样才能这样使用paypal_credentials?我找不到有关customer_paypal_credentials的前缀/后缀功能的任何信息。
谢谢您。

最佳答案

这在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

关于ruby-on-rails - 多个具有相同键的store_accessors,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47282156/

10-13 02:15