# Wanted: has\_many :through elegance

**URL:** <https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942>\
**Category:** Rails\
**Created:** [October 12, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942 "2006-10-12T16:54:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [October 12, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942/1 "2006-10-12T16:54:50Z")

</div>

as join\_table bs

* * *

id \<------. id ,----\> id  
name `—\*\> a\_id ,’ name  
b\_id \<–’  
holy\_graal

1. I’m trying to find the most clean and elegent way of accessing  
holy\_graal given an instance of a and b. I refuse to believe the only  
way is to do a cold search like Join\_table.find (:first, :conditions =\>  
insert a.id and b.id here)

(In my case, join\_table can only contain one combination of a\_id and  
b\_id.)

1. Furthermore, I’ve read I shouldn’t use the \<\< operator for storing  
when in has\_many :through relations. I’ve also read I shouldn’t use  
push\_with\_attributes. So what should I use?

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [October 13, 2006, 12:28am UTC](https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942/2 "2006-10-13T00:28:17Z")

</div>

One more question: I read I should be able to do…  
a.holy\_graal  
…if I just add the line…  
has\_many :bs, :through =\> :join\_table, :select =\>  
“join\_table.holy\_graal, b.\*”  
…to the class declaration for A.

I can’t get this working though. Is there anything more to it I might be  
missing?

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [October 13, 2006, 1:13am UTC](https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942/3 "2006-10-13T01:13:01Z")

</div>

Vincent wrote:

> One more question: I read I should be able to do…  
> a.holy\_graal  
> …if I just add the line…  
> has\_many :bs, :through =\> :join\_table, :select =\>  
> “join\_table.holy\_graal, b.\*”  
> …to the class declaration for A.
> 
> I can’t get this working though. Is there anything more to it I might be  
> missing?

You don’t need the :select option. the :through option takes care of  
all that for you. I have a lot of examples on my site. Check out this  
one:

[http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation](http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation)

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

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [October 13, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/wanted-has-many-through-elegance/71942/4 "2006-10-13T13:53:24Z")

</div>

Great reading Josh, bookmarked it as well.  
That takes care of my q#2.

In my dream language (which has yet to be invented), the answer to the  
question “most elegant way to connect two instances via a join\_table  
with an attribute” would be:

a \<\< attribute \>\> b

or

a \<\< attribute1, attribute2 \>\> b

Is there anyway I can override the \<\< operator so that I can associate  
instance a with b with a join table attribute?
