Forum: Ruby on Rails has_many :trough with 2x the same table

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.
Martijn (Guest)
on 2006-04-03 23:46
(Received via mailing list)
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?
Dan P. (Guest)
on 2006-04-04 01:57
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
This topic is locked and can not be replied to.