Has_many :trough with 2x the same table


#1

I have a site where Users can become Friends with other Users. I used to
do this with a habtm, but of course I want to stay up-to-date and
convert this to a has_many :trough. Unfortunately I can’t get it to
work… the documentation on this feature is a bit sparse at the moment.

This is my old habtm:

has_and_belongs_to_many :friends, :join_table => ‘user_friends’,
:foreign_key => ‘user_id’, :association_foreign_key => ‘friend_id’,
:class_name => ‘Person’

I’ve tried various things, but I’m completely lost now. I looked at the
:source option, but that doesn’t allow me to do this and the :class_name
parameter doesn’t seem to work either. Any hints?


#2

Martijn wrote:

I have a site where Users can become Friends with other Users. I used to
do this with a habtm, but of course I want to stay up-to-date and
convert this to a has_many :trough. Unfortunately I can’t get it to
work… the documentation on this feature is a bit sparse at the moment.

This is my old habtm:

has_and_belongs_to_many :friends, :join_table => ‘user_friends’,
:foreign_key => ‘user_id’, :association_foreign_key => ‘friend_id’,
:class_name => ‘Person’

I’ve tried various things, but I’m completely lost now. I looked at the
:source option, but that doesn’t allow me to do this and the :class_name
parameter doesn’t seem to work either. Any hints?

Maybe I’m wrong about this, but the point of :through is not to replace
HABTM. You use it when your join table represents a different class
that has its own ID and attributes. If your join table is just a join
table (2 ids which are foreign keys to the other 2 tables) then stick to
HABTM.

Dan