# Self-referential :has\_many :through relationship?

**URL:** <https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575>\
**Category:** Rails\
**Created:** [October 29, 2007, 3:47am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575 "2007-10-29T03:47:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [October 29, 2007, 3:47am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575/1 "2007-10-29T03:47:56Z")

</div>

Hi All,

I’m building an app that needs to make a comparison between two people  
and I’m wondering how to best build the models, and if I’m doing this  
correctly.

I’m representing each partner as a User, so I wanted to keep just a user  
table and a comparisons table (the comparisons table will keep all the  
comparison data, the user model will hold all the data about that  
individual user)

I’m thinking of making a User-to-User self-referential join through a  
“comparison” model.

So, I’d have two models: the user model, and a comparison model

The user model would look something like this:  
class User \< ActiveRecord::Base  
belongs\_to :partner, :through =\> :comparisons  
end

and the comparisons:

class Comparison \< ActiveRecord::Base  
belongs\_to :user  
has\_one :partner, :class\_name =\> “User”, :foreign\_key =\> “partner\_id”  
end

I honestly have no idea if this will work… does this look close?

Thanks in advance!  
Dustin

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [October 30, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575/2 "2007-10-30T20:18:13Z")

</div>

Dustin A. wrote:

> I’m building an app that needs to make a comparison between two people  
> and I’m wondering how to best build the models, and if I’m doing this  
> correctly.
> 
> I’m representing each partner as a User, so I wanted to keep just a user  
> table and a comparisons table (the comparisons table will keep all the  
> comparison data, the user model will hold all the data about that  
> individual user)
> 
> I’m thinking of making a User-to-User self-referential join through a  
> “comparison” model.
> 
> So, I’d have two models: the user model, and a comparison model
> 
> The user model would look something like this:  
> class User \< ActiveRecord::Base  
> belongs\_to :partner, :through =\> :comparisons  
> end
> 
> and the comparisons:
> 
> class Comparison \< ActiveRecord::Base  
> belongs\_to :user  
> has\_one :partner, :class\_name =\> “User”, :foreign\_key =\> “partner\_id”  
> end
> 
> I honestly have no idea if this will work… does this look close?

Did you get this to work yet? I just posted an updated example of a  
self-referential has\_many :through on my blog:  
[http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through](http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through)  
You might find that helpful.

If you really need an asymmetric join model, I’m interested in seeing  
what you put together. I was thinking about blogging an example of that  
too, but I couldn’t think up a good use for it.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [October 29, 2007, 4:23am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575/3 "2007-10-29T04:23:49Z")

</div>

On 10/28/07, Dustin A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> individual user)
> 
> I’m thinking of making a User-to-User self-referential join through a  
> “comparison” model.
> 
> So, I’d have two models: the user model, and a comparison model
> 
> The user model would look something like this:  
> class User \< ActiveRecord::Base  
> belongs\_to :partner, :through =\> :comparisons

:through is not a valid option on belongs\_to, you can only use it on  
:has\_many

…

> I honestly have no idea if this will work… does this look close?

Sorry, don’t think so.

## Maybe with a little more insight on what you’re trying to do with the comparison Model, someone can move you towards the goal.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [October 30, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-relationship/118575/4 "2007-10-30T21:57:18Z")

</div>

> Did you get this to work yet? I just posted an updated example of a  
> self-referential has\_many :through on my blog:  
> [has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through)  
> You might find that helpful.
> 
> If you really need an asymmetric join model, I’m interested in seeing  
> what you put together. I was thinking about blogging an example of that  
> too, but I couldn’t think up a good use for it.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Well, what I’m trying to do is compare two people - through a photo…  
I’m using attachment\_fu. Here’s what I have at this point… I actually  
did away with the User model all together and just made the comparisons  
hold all the foreign keys:

Comparisons model:  
belongs\_to :male\_partner, :polymorphic =\> true  
belongs\_to :female\_partner, :polymorphic =\> true

(Fields for comparisons):  
t.column “male\_partner\_id”, :integer  
t.column “male\_partner\_type”, :string  
t.column “female\_partner\_id”, :integer  
t.column “female\_partner\_type”, :string  
t.column “comparison\_value”, :float

MalePartner  
has\_many :comparisons, :as =\> :male\_partner

FemalePartner  
has\_many :comparisons, :as =\> :female\_partner

That’s it, again, no idea if it will work… The reason I made the  
male\_partner and female\_partner polymorphic is to be able to use an  
image from the web instead of an uploaded image. This is for a dating  
site.

Thanks for the blog Josh, I checked it out… very cool - a little more  
that I need for this (I think), but nonetheless, it’s cool to see that  
all those self-referential joins can be done.
