toddlc
April 9, 2008, 10:06pm
1
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
toddlc
April 9, 2008, 10:10pm
2
On Apr 9, 4:06 pm, Todd C. [email protected] wrote:
When I
type.attributes << attribute
it goes in the owner_id column.
!!! This should say: “it goes in the type_id
column” !!!
toddlc
April 9, 2008, 10:30pm
3
Todd C. 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
toddlc
April 10, 2008, 1:45am
4
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/
toddlc
April 21, 2008, 3:35pm
5
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