# Adding objects to a :through association

**URL:** <https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367>\
**Category:** Rails\
**Created:** [March 31, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367 "2006-03-31T20:43:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/1 "2006-03-31T20:43:42Z")

</div>

So I’m one of those nasty people building a self-referential  
habtm-like Association using the funky new :through stuff. This is  
about users having friends, so here’s my user.rb:

class User \< ActiveRecord::Base  
has\_many :friendships, :foreign\_key =\> ‘user\_id’  
has\_many :friends, :through =\> :friendships, :source =\> :friend  
end

And here’s my friendship.rb:

class Friendship \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :friend, :class\_name =\> ‘User’, :foreign\_key =\> ‘friend\_id’  
end

Now, everything is working great as long as I create instances of  
Friendship “manually”. Really no problems whatsoever, everything is  
working just perfectly.

However, simply moving one user into another user’s list of friends  
does _not_ seem to work:

someone.friends \<\< someoneelse # nothing happens

Considering I’m not seeing any notes about collection.\<\< not working  
with :through associations, I’m wondering if this is specific to crazy  
self-referential stuff like what I’m doing.

Any hints?

Danke!

–

> **[Hendrik Mans](https://hendrik.mans.de/)**
>
> Hendrik Mans: Freelancer in Hamburg. Ruby on Rails, Elixir, Phoenix, JavaScript, React.js, Vue.js., Elm.

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/2 "2006-03-31T20:56:17Z")

</div>

Hendrik M. wrote:

> So I’m one of those nasty people building a self-referential  
> habtm-like Association using the funky new :through stuff. This is  
> about users having friends, so here’s my user.rb:
> 
> class User \< ActiveRecord::Base  
> has\_many :friendships, :foreign\_key =\> ‘user\_id’  
> has\_many :friends, :through =\> :friendships, :source =\> :friend  
> end
> 
> And here’s my friendship.rb:
> 
> class Friendship \< ActiveRecord::Base  
> belongs\_to :user  
> belongs\_to :friend, :class\_name =\> ‘User’, :foreign\_key =\> ‘friend\_id’  
> end
> 
> Now, everything is working great as long as I create instances of  
> Friendship “manually”. Really no problems whatsoever, everything is  
> working just perfectly.
> 
> However, simply moving one user into another user’s list of friends  
> does _not_ seem to work:
> 
> someone.friends \<\< someoneelse # nothing happens
> 
> Considering I’m not seeing any notes about collection.\<\< not working  
> with :through associations, I’m wondering if this is specific to crazy  
> self-referential stuff like what I’m doing.
> 
> Any hints?

I don’t think you can create a join model record automatically by adding  
to a :through association. You have to create the friendship manually.  
When you think about it, that makes sense, since the main point of using  
a join model is to put other attributes in it. There’s nowhere to  
specify those attributes the way you’re trying to do it.

Try doing someone.friendships.create(:friend =\> friend\_user) and see if  
that works.

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

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/3 "2006-03-31T21:08:09Z")

</div>

On 3/31/06, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try doing someone.friendships.create(:friend =\> friend\_user) and see if  
> that works.

Yes, that works. I was just confused by the fact that there was no  
mention of \<\< not working with :through associations.

> When you think about it, that makes sense, since the main point of using  
> a join model is to put other attributes in it. There’s nowhere to  
> specify those attributes the way you’re trying to do it.

Yes, I generally agree. However, using :through already has a bunch of  
advantages over simple habtm even without “rich” attributes that I  
would want to fill manually. In this scenario, the only extra  
attribute that I want is created\_at, which is set automatically. I was  
assuming that AR would create the association model instance  
automatically, considering it could easy infer the class name etc.  
from the given parameters.

Anyway, user.friendships.create() will work, I was just wondering  
really.

Thanks,  
Hendrik

–

> **[Hendrik Mans](https://hendrik.mans.de/)**
>
> Hendrik Mans: Freelancer in Hamburg. Ruby on Rails, Elixir, Phoenix, JavaScript, React.js, Vue.js., Elm.

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/4 "2006-03-31T21:41:49Z")

</div>

Hi Hendrik,

as far as I know, :through associations are read-only.

It makes sense because the model you are going :through is  
completely standalone and as such, it may have more than  
just the two belongs\_to associations you specify.

I.e. the has\_many :through directive doesn’t capture enough information  
to reliably create the model you are going :through.

## Makes sense?

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/5 "2006-03-31T21:53:58Z")

</div>

Lets say your association table has a position field – and you need  
to order by the position and you also need to be able to update the  
position hows that handled?

Chris

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [April 6, 2006, 6:55am UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/6 "2006-04-06T06:55:14Z")

</div>

It’s not specific to what you’re doing… I had the same problem. I  
may have this wrong, but I think :through associations are strictly  
read-only. What I’d do:

someone.friendships.create(:friend =\> someone\_else)

This is a little more pedantic, but closer to what’s happening in the  
database and perhaps your Friendship model: when you add someone to  
your list of friends, you’re creating a field in the friendships  
table. HABTM associations might allow you to use the line I’ve quoted?

– Michael D.

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [April 6, 2006, 4:19am UTC](https://www.ruby-forum.com/t/adding-objects-to-a-through-association/93367/7 "2006-04-06T04:19:28Z")

</div>

Hendrik,  
Out of curiosity, what does your schema look like for Friendships?

Hendrik M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: So I’m one of those nasty people  
building a self-referential  
habtm-like Association using the funky new :through stuff. This is  
about users having friends, so here’s my user.rb:

class User \< ActiveRecord::Base  
has\_many :friendships, :foreign\_key =\> ‘user\_id’  
has\_many :friends, :through =\> :friendships, :source =\> :friend  
end

And here’s my friendship.rb:

class Friendship \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :friend, :class\_name =\> ‘User’, :foreign\_key =\> ‘friend\_id’  
end

Now, everything is working great as long as I create instances of  
Friendship “manually”. Really no problems whatsoever, everything is  
working just perfectly.

However, simply moving one user into another user’s list of friends  
does _not_ seem to work:

someone.friends \<\< someoneelse # nothing happens

Considering I’m not seeing any notes about collection.\<\< not working  
with :through associations, I’m wondering if this is specific to crazy  
self-referential stuff like what I’m doing.

Any hints?

Danke!

–

> **[Hendrik Mans](https://hendrik.mans.de/)**
>
> Hendrik Mans: Freelancer in Hamburg. Ruby on Rails, Elixir, Phoenix, JavaScript, React.js, Vue.js., Elm.
