# HABTM - how to insert join row when associated rows exist

**URL:** <https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170>\
**Category:** Rails\
**Created:** [April 29, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170 "2006-04-29T20:26:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [April 29, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/1 "2006-04-29T20:26:32Z")

</div>

I have successfully used HABTM to create a many-to-one-to-many set of  
rows in one step. Now, if two objects I want to associate already exist,  
how do I create the join row? The Agile book mentions the  
‘push\_with\_attributes’ method. The text says this method is useful for  
adding additional attributes to the join row (in the example a ‘read\_at’  
timestamp). I only require a simple two-key join row - is  
‘push\_with\_attributes’ the way to go?

-Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [April 29, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/2 "2006-04-29T20:30:48Z")

</div>

Lindsay B. wrote:

> many-to-one-to-many set of rows

should read ’ one-to-many-to-one rows’

-Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [April 29, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/3 "2006-04-29T22:02:10Z")

</div>

It’s OK, I figured it out.

-Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [April 30, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/4 "2006-04-30T15:54:25Z")

</div>

> Please end your monolog in a proper way by stating your solution.

A bug in my model’s validation prevented the join row from being  
created. No solution as such.

- Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [May 3, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/5 "2006-05-03T19:09:39Z")

</div>

push with attributes is not what you want to use. that’s for when you  
have  
additional columns in your join table other than the two id columns for  
the  
joined tables.

example of what you want to do:

assumiing a habtm between foo and bar (using table bars\_foos as join  
tabled)

foo = Foo.find(1)  
bar = Bar.find(1)

foo.bars \<\< bar

# or you could do bar.foos \<\< foo, same results

this will create the association in the join table.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000533)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

Chris

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [April 30, 2006, 11:41am UTC](https://www.ruby-forum.com/t/habtm-how-to-insert-join-row-when-associated-rows-exist/59170/6 "2006-04-30T11:41:58Z")

</div>

Lindsay B. wrote:

> It’s OK, I figured it out.
> 
> -Lindsay

Please end your monolog in a proper way by stating your solution.
