views:

170

answers:

1

I have two Models, Modela and Modelb.

Modela can only own one Modelb, but Modelb can be a part of many Modela's.

What I have right now is

class Modela < ActiveRecord::Base
  has_one :modelb
end

class Modelb < ActiveRecord::Base
  belongs_to :modela, :foreign_key => "modela_id" #might not make sense?
end

Not too sure about the whole :foreign_key thing I was doing there, but it was where it was when I left off. As I am trying to allow Modelb to be part of many Modela's, I don't want to add a modela_id field to the Modelb table.

What is the best way to do this?

+2  A: 

It should be:

class Modela
  belongs_to :modelb
end

class Modelb
  has_many :modela
end

And modelas table should contain modelb_id column.

klew
Thanks, I was actually just in the middle of trying that!
Lowgain