2017-02-27 83 views
0

我使用Rails 5,Rails的管理和MongoId,在我的模型,我加has_and_belongs_to_many和foreign_key並不在Rails的管理工作+ 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'。你有任何想法來解決它?

回答

0

我解決我的問題,通過一個別名字段添加到模型:

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