Wanted: has_many :through elegance

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?

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?

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


Josh S.
http://blog.hasmanythrough.com

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?