Forum: Ruby on Rails HowToHandleMultipleRelationshipsBetweenTables?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and 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"

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

How can the "belongs_to" argument be anything but ":relationshiptype".

Can any one clue me in on how to get around this.

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
of the association.
 See here:

You want something like this. I'd use RelationshipType instead of
Relationshiptype because it is two words in english, and also wouldn't
little abbreviations like you have, they just make the code harder to

class RelationshipType < ActiveRecord:Base

class Relationship < ActiveRecord::Base
  belongs_to :relationship_type # :class_name defaults to
based on first argument, :foreign_key defaults to :relationship_type_id
  belongs_to :inverse_relationship_type, :class_name =>
:foreign_key => 'inverse_relationship_type_id'

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.