Ratatouille
Ratatouille

Reputation: 1492

Have foreign_key on mongoid

Well I'm have a PerformerSource and MonthlyEarning documents which has a field called performer_id

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


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

Now all I want to set a has_many relationships between the two documents with performer_id in mind i.e

  performer_source has_many monthly_earnings
  monthly_earning belongs_to performer_source

I think the following isn't allowed in Mongoid because apparently when I set the relationships it just does not return anything

But if it does then please let me know

Upvotes: 0

Views: 102

Answers (1)

Ratatouille
Ratatouille

Reputation: 1492

Apparently this work so the idea was to have relations through performer_id field present in both document so all that is need is

to set this

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

The way it works over here is the :primary_key i.e(performer_id) when firing the request through association

This is exactly what I want

Upvotes: 1

Related Questions