has_and_belongs_to_many和foreign_key在Rails Admin + MongoID中不起作用

时间:2017-02-27 09:30:07

标签: mongoid rails-admin

我正在使用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'。你有什么想法来解决它吗?

1 个答案:

答案 0 :(得分:0)

我通过在模型中添加别名字段来解决我的问题:

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