Christopher Biltoft
2006-Mar-13 11:51 UTC
[Rails] HowToHandleMultipleRelationshipsBetweenTables?
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 -- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-Mar-13 11:59 UTC
[Rails] HowToHandleMultipleRelationshipsBetweenTables?
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/Associations/ClassMethods.html#M000473 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 3/14/06, Christopher Biltoft <cbiltoft@forexplus.com.au> wrote:> > 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/ae83380a/attachment-0001.html