# HELP with self-referential join (using another model as join table)

**URL:** <https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571>\
**Category:** Rails\
**Created:** [July 20, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571 "2008-07-20T18:42:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [July 20, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571/1 "2008-07-20T18:42:20Z")

</div>

Hi all,

I’m having problems with the following relationship, can anybody help  
me out here? I’m using Rails 2.1.

Cheers, Sazima

# Table name: votes

# 

# Field Type Null Default

# id int(11) No auto\_increment

# voter\_id int(11) No

# voted\_id int(11) No

# type int(11) No 0

# value varchar(255) Yes NULL

# status int(11) Yes 0

# created\_at datetime Yes NULL

# updated\_at datetime Yes NULL

# 

class Vote \< ActiveRecord::Base

belongs\_to :voter, :class\_name =\> :user, :foreign\_key =\> :voter\_id  
belongs\_to :voted, :class\_name =\> :user, :foreign\_key =\> :voted\_id

end

# 

* * *

# Table name: users

# 

# id :integer(11) not null, primary key

# login :string(255)

# crypted\_password :string(40)

# salt :string(40)

# created\_at :datetime

# updated\_at :datetime

class User \< ActiveRecord::Base

# Votes

has\_many :votes\_made, :class\_name =\> :vote, :foreign\_key  
=\> :voter\_id  
has\_many :votes\_received, :class\_name =\> :vote, :foreign\_key  
=\> :voted\_id  
has\_many :votees, :through =\> :votes\_made, :source =\> :user, :uniq  
=\> true  
has\_many :voters, :through =\> :votes\_received, :source  
=\> :user, :uniq =\> true

end

---

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [July 20, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571/2 "2008-07-20T20:42:45Z")

</div>

On 20 Jul 2008, at 17:41, Sazima wrote:

> Hi all,
> 
> I’m having problems with the following relationship, can anybody help  
> me out here? I’m using Rails 2.1.

You don’t say exactly what problems you’re having, so just some quick  
comments.

> # status int(11) Yes 0
> 
> # created\_at datetime Yes NULL
> 
> # updated\_at datetime Yes NULL
> 
> # 
> 
> class Vote \< ActiveRecord::Base  
> belongs\_to :voter, :class\_name =\> :user, :foreign\_key =\> :voter\_id  
> belongs\_to :voted, :class\_name =\> :user, :foreign\_key =\> :voted\_id  
> end

Can’t remember off the top of my head whether it really matters but  
that should have :class\_name =\> ‘User’.  
You don’t need the foreign key option here - the default is assocation  
name + \_id

Your type column will cause problems: rails uses the type column for  
single table inheritance, as it is you’ll probably get some horrendous  
error whenever you try and load an  
instance of Vote.

> # updated\_at :datetime
> 
> class User \< ActiveRecord::Base
> 
> # Votes
> 
> has\_many :votes\_made, :class\_name =\> :vote, :foreign\_key  
> =\> :voter\_id  
> has\_many :votes\_received, :class\_name =\> :vote, :foreign\_key  
> =\> :voted\_id

That should probably also be :class\_name =\> ‘Vote’

> has\_many :votees, :through =\> :votes\_made, :source =\> :user, :uniq  
> =\> true  
> has\_many :voters, :through =\> :votes\_received, :source  
> =\> :user, :uniq =\> true

You’re not using the :source option correctly. What you’re telling  
rails is 'so load the votes\_made association, then grab the  
association named x (the default would be votee).  
So you source option here should be :voter or :voted

Fred

---

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [July 20, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571/3 "2008-07-20T23:41:22Z")

</div>

Hey Fred,

Thanks a lot for the comments. I had already tried ‘User’ and ‘Vote’,  
as well as commented the “has\_many … through” statements, but didn’t  
thought of the type conflict. That was it! Thanks!

Cheers, Sazima

P.S.: That would be a nice addition to the migration mechanism: an  
alert when a column with such a name is used…

On Jul 20, 3:42 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [July 21, 2008, 12:11am UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143571/4 "2008-07-21T00:11:49Z")

</div>

And yeah, you were also right about the :user x ’ User’ and :source  
things…

Cheers, Sazima
