Ruby on rails Mongoid:4.x如何将ID数组作为1-N引用?
我有两节课Ruby on rails Mongoid:4.x如何将ID数组作为1-N引用?,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,我有两节课 class Round include Mongoid::Document include Mongoid::Attributes::Dynamic has_many :users end class User include Mongoid::Document include Mongoid::Attributes::Dynamic belongs_to :round end 这一切都很好,但我想要的是圆形模型上的两种方法 好用户和坏用户都与用户类有
class Round
include Mongoid::Document
include Mongoid::Attributes::Dynamic
has_many :users
end
class User
include Mongoid::Document
include Mongoid::Attributes::Dynamic
belongs_to :round
end
这一切都很好,但我想要的是圆形模型上的两种方法
好用户和坏用户都与用户类有关。我希望重用用户类,但具有独特的方法访问权限
我知道经典模型是在用户上使用一个字段,将其与round关联,因此用户在数据库中有一个round_id字段。但是我可以在哪里安装。Round类有两个字段,用户ID作为数组存储在其中
这是我可以通过mongoid实现的现成功能吗?这将在用户中为这两个关系创建两列
round.rb
has_many :good_users, class_name: 'User', inverse_of: :good_in_round
has_many :bad_users, class_name: 'User', inverse_of: :bad_in_round
user.rb
belongs_to :good_in_round, class_name: 'Round', inverse_of: :good_users
belongs_to :bad_in_round, class_name: 'Round', inverse_of: :bad_users
Mongoid将相关对象ID存储为多对多关系中的数组。所以你也可以在这里使用它
round.rb
has_and_belongs_to_many :good_users, class_name: 'User', inverse_of: :good_in_round
has_and_belongs_to_many :bad_users, class_name: 'User', inverse_of: :bad_in_round
我是否需要在回合中同时使用has_many和has_和has_besides_to_many。rb?不,只使用一个。如果要将ID保存为数组,请使用has_和reasons_to_many。