0

好吧,我有一个文件PerformerSourceMonthlyEarning其中有一个名为performer_id

class PerformerSource
  ....
  field :performer_id,:type => Integer
  ....
  ....
end


class MonthlyEarning
  ....
  field :performer_id,:type => Integer
  ....
  ....
end

现在我只想设置has_many两个文档之间的关系,并记住 performer_id 即

  performer_source has_many monthly_earnings
  monthly_earning belongs_to performer_source

我认为在 Mongoid 中不允许以下内容,因为显然当我设置关系时它不会返回任何内容

但如果确实如此,请告诉我

4

1 回答 1

1

显然,这项工作的想法是通过两个文档中都存在的 performer_id 字段建立关系,所以只需要

设置这个

class PerformerSource
  ....
  field :performer_id,:type => Integer
  ....
  ....
  has_many :earnings ,:class_name => "MonthlyEarning",:primary_key => :performer_id,:foreign_key => :performer_id
end

class MonthlyEarning
   ....
  field :performer_id,:type => Integer
  ....
  ....
  belongs_to :performer,:class_name => "PerformerSource",:primary_key => :performer_id,:foreign_key => :performer_id
end

它在这里的工作方式是通过关联触发请求时的:primary_keyie( )performer_id

这正是我想要的

于 2013-10-16T06:54:14.923 回答