# How do you access the :through join model objects?

**URL:** <https://www.ruby-forum.com/t/how-do-you-access-the-through-join-model-objects/117867>\
**Category:** Rails\
**Created:** [October 21, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/how-do-you-access-the-through-join-model-objects/117867 "2007-10-21T21:09:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![avramd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@avramd](https://www.ruby-forum.com/u/avramd)\
**Post date:** [October 21, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/how-do-you-access-the-through-join-model-objects/117867/1 "2007-10-21T21:09:48Z")

</div>

Hello,

I have some confusion regarding how access the join model object that is  
responsible for a relationship I am working with. Here’s a simple case:

class A \< ActiveRecord::Base  
has\_many :connections  
has\_many :bs, :through =\> :connections  
end

class B \< ActiveRecord::Base  
has\_many :connections  
has\_many :as, :through =\> :connections  
end

class Connection \< ActiveRecord::Base  
belongs\_to :a  
belongs\_to :b  
end

Now say I’ve already got my hands on an A, called a, and one of it’s  
b’s. So, I can already say a.bs[b\_index].attribute = value. That’s  
great. But where is the Connection object that is tying this a to this  
particular b? The object must exist. Let’s say I have a reference to it  
called c, so that I can say c.attribute = value. But how do I get that  
reference?

The only way I can see to do it is to find it manually via it’s own  
class’s relationship, as in a.connections.find\_by\_b\_id(b.id). But that  
seems very WET (i.e. not DRY) to me; I shouldn’t have to do a search for  
an object, that I’m essentially already using.

FWIW, I’ve been searching on this forever, so I apologize if the answer  
is out there & I’ve simply failed to find it.

TIA,  
Avram

---

<div class="post-metadata">

**Author:** ![avramd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@avramd](https://www.ruby-forum.com/u/avramd)\
**Post date:** [October 21, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/how-do-you-access-the-through-join-model-objects/117867/2 "2007-10-21T21:18:11Z")

</div>

a quick additional comment:

I would have thought I could say a.bs[i].connections and get _just_ an  
array of the connection objects that tie b[i] to a. But instead it seems  
that I get all of bs[i]'s connections, whether or not they connect b to  
a. I would think there would be a simple way to refer to just the  
objects that join a specific b to a specific a, without having to  
explicitly search for them. Is there?

---

<div class="post-metadata">

**Author:** ![avramd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@avramd](https://www.ruby-forum.com/u/avramd)\
**Post date:** [October 22, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/how-do-you-access-the-through-join-model-objects/117867/3 "2007-10-22T12:24:11Z")

</div>

Avram D. wrote:

> has\_many :connections  
> great. But where is the Connection object that is tying this a to this  
> particular b? The object must exist. Let’s say I have a reference to it  
> called c, so that I can say c.attribute = value. But how do I get that  
> reference?
> 
> The only way I can see to do it is to find it manually via it’s own  
> class’s relationship, as in a.connections.find\_by\_b\_id(b.id). But that  
> seems very WET (i.e. not DRY) to me; I shouldn’t have to do a search for  
> an object, that I’m essentially already using.

Usual way is:

a = A.find(:first, :conditions =\> ‘…’, :include =\> {:connections =\>  
:b})

a.connections[b\_index].attribute = value  
a.connections[b\_index].b.attribute = value

–  
We develop, watch us RoR, in numbers too big to ignore.
