Forum: Ruby on Rails HowToHandleMultipleRelationshipsBetweenTables?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2f03223d980593a583225f94fa44ed15?d=identicon&s=25 Christopher Biltoft (fxp)
on 2006-03-13 12:49
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
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-13 13:01
(Received via mailing list)
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.
2f03223d980593a583225f94fa44ed15?d=identicon&s=25 Christopher Biltoft (fxp)
on 2006-03-13 13:36
Ah Ha - many thanks, regards Christopher
This topic is locked and can not be replied to.