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.
Christopher B. (Guest)
on 2006-03-13 13: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.

Jonathan V. (Guest)
on 2006-03-13 14: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'

Christopher B. (Guest)
on 2006-03-13 14:36
Ah Ha - many thanks, regards Christopher
This topic is locked and can not be replied to.