有关于mongoid的foreign_key

时间:2013-10-15 15:56:10

标签: mongoid mongoid3

我有一个PerformerSourceMonthlyEarning个文档,其中有一个名为performer_id的字段

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


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

现在我想在performer_id中设置两个文件之间的has_many关系,即

  performer_source has_many monthly_earnings
  monthly_earning belongs_to performer_source

我认为在Mongoid中不允许使用以下内容,因为显然我设置了关系  它只是没有返回任何东西

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

1 个答案:

答案 0 :(得分: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_key,即(performer_id)通过关联解雇请求时

这正是我想要的