I have two tables where one of the tables contains two columns that reference the id of the other. This problem is partially described in the "HowTo..." with the topic title above. My model looks like this: class Relationship < ActiveRecord::Base belongs_to :relationshiptype, :foreign_key => "relnshiptype" belongs_to :relationshiptype, :foreign_key => "inverserelnshiptype" end It doesn't work because I think you can't specify the same model valiable? "relationshiptype" more than once. The table is "relationshiptype" and the Class is therefore "Relationshiptype". How can the "belongs_to" argument be anything but ":relationshiptype". Can any one clue me in on how to get around this. Regards
on 2006-03-13 12:49
on 2006-03-13 13:01
The first argument to belongs_to is not the name of a model, it's the name of the association. See here: http://api.rubyonrails.org/classes/ActiveRecord/As... You want something like this. I'd use RelationshipType instead of Relationshiptype because it is two words in english, and also wouldn't do little abbreviations like you have, they just make the code harder to read. class RelationshipType < ActiveRecord:Base end class Relationship < ActiveRecord::Base belongs_to :relationship_type # :class_name defaults to RelationshipType based on first argument, :foreign_key defaults to :relationship_type_id belongs_to :inverse_relationship_type, :class_name => 'RelationshipType', :foreign_key => 'inverse_relationship_type_id' end -Jonathan.
on 2006-03-13 13:36
Ah Ha - many thanks, regards Christopher