Type with two references to the same table

Hi all,

Struggling with the case of one type with two refs to another type:

Type   has_many :attributes

Attribute   belongs_to :owner, :foreign_key => "owner_id", :class_name = "Type"   has_one :type

When I

   type.attributes << attribute

it goes in the owner_id column.

I can't find any examples of this on the web, looking for any hints.

Thanks,

Todd

Hi all,

Struggling with the case of one type with two refs to another type:

Type has_many :attributes

Attribute belongs_to :owner, :foreign_key => "owner_id", :class_name = "Type" has_one :type

When I

type.attributes << attribute

it goes in the owner_id column.

!!! This should say: "it goes in the type_id column" !!!

Todd Chambery wrote:

!!! This should say: "it goes in the type_id column" !!!

You need to tell the has_many relationship about the foreign key, too:

has_many :attributes, :foreign_key => :owner_id

It's one of the quirks of Rails, unfortunately.

Also, I'd be wary of using "Type" for anything to do with an ActiveRecord model. If you start getting strange errors, then have a look here:

http://wiki.rubyonrails.org/rails/pages/ReservedWords

Yeah, type is used by Single Table Inheritance.

Julian

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW! http://sensei.zenunit.com/

Thank you, thank you (both)! Changed my names to Entity and Attrib (I think attribute is also reserved). Never would have figured out the :foreign_key business on my own.

Thanks again,

Todd