【问题标题】:has_and_belongs_to_many and foreign_key does not work in Rails Admin + MongoIDhas_and_belongs_to_many 和 foreign_key 在 Rails Admin + MongoID 中不起作用
【发布时间】:2017-02-27 09:30:06
【问题描述】:

我正在使用 Rails 5、Rails Admin 和 MongoId,在我的模型中,我添加了

has_and_belongs_to_many :topics, class_name: 'Topic', :foreign_key => :topicIds 

它适用于“编辑页面”,但当我尝试更改 topicIds 的值时不起作用

message: Attempted to set a value for 'topic_ids' which is not allowed on the model AppUser. summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call AppUser#topic_ids= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. resolution: You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.

表示 MongoId 不完全理解 ':foreign_key'。你有什么解决办法吗?

【问题讨论】:

    标签: mongoid rails-admin


    【解决方案1】:

    我通过向模型添加别名字段解决了我的问题:

    field :topicIds, type: Array, :as => :topic_ids
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多