# HABTM problem not saving all associations

**URL:** <https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752>\
**Category:** Rails\
**Created:** [January 6, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752 "2006-01-06T18:51:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keith\_Veleba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e68b1a/32.png) [@Keith\_Veleba](https://www.ruby-forum.com/u/Keith_Veleba)\
**Post date:** [January 6, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752/1 "2006-01-06T18:51:37Z")

</div>

Hello all,

I have an Order object that has and belongs to many Products and has  
and belongs to many Loan Types. This is so I can select multiples of  
each in my order entry screen via checkbox groups.

I’m having some trouble with saving multiple HABTM associations for a  
single model object; only the first HABTM association declared in the  
model will save during the initial @order.save in the create action in  
my orders\_controller.rb code below.

If the order is then updated via an edit action, all selected  
associations will save correctly. If you notice in the  
development.log dump, I’m passing product\_ids and loan\_type\_ids arrays  
back to the controller properly. Inspecting the @order object during  
a breakpointer session shows the related object are available for  
saving, but for some reason, ActiveRecord does not want to save the  
second association class at the beginning.

Any ideas what I’m doing wrong?

I’m using Ruby 1.8.2, Rails 1.0 and MySQL 5.0.16 on both Ubuntu Breezy  
and Mac OS X Tiger 10.4.3. Ruby and MySQL were built from source on  
both as well.

Thanks for your time and attention.

Keith Veleba

—CUT HERE—

class Order \< ActiveRecord::Base  
belongs\_to :account  
has\_and\_belongs\_to\_many :loan\_types  
has\_and\_belongs\_to\_many :products  
has\_many :order\_notes  
has\_one :structure\_type  
has\_one :order\_state  
end

class Product \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :orders  
end

class LoanType \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :orders  
end

chunk of orders\_controller.rb:

def create  
@order = Order.new(params[:order])  
@order.products = Product.find(params[:product\_ids]) if  
params[:product\_ids]  
@order.loan\_types = LoanType.find(params[:loan\_type\_ids]) if  
params[:loan\_type\_ids]

```
if @order.save
  flash[:notice] = 'Order was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

def update  
@order = Order.find(params[:id])  
@order.products = Product.find(params[:product\_ids]) if  
params[:product\_ids]  
@order.loan\_types = LoanType.find(params[:loan\_type\_ids]) if  
params[:loan\_type\_ids]  
if @order.update\_attributes(params[:order])  
flash[:notice] = ‘Order was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @order  
else  
render :action =\> ‘edit’  
end  
end

chunk of \_form.rhtml for orders:

**Loan Type _(check all that apply)_** \<% for product in @products %\> checked="checked"\<% end %\> \> \<%= product.title %\>   

\<% end %\>

**Products _(check all that apply)_** \<% for loan\_type in @loan\_types %\> checked="checked"\<% end %\> \> \<%= loan\_type.name %\>   

\<% end %\>

chunk of log/development.log:

Processing OrdersController#create (for 127.0.0.1 at 2006-01-05  
13:15:11) [POST]  
Parameters: {“commit”=\>“Create”, “product\_ids”=\>[“1”, “2”, “3”],  
“loan\_type\_ids”=\>[“1”, “2”, “4”], “order”=\>{“city”=\>“”,  
“loan\_id\_2”=\>“”, “borrower\_first\_name”=\>“”, “borrower\_last\_name”=\>“”,  
“loan\_id\_3”=\>“”, “zip”=\>“”, “account\_id”=\>“10000”,  
“structure\_type\_id”=\>“1”, “builder\_name”=\>“”,  
“coborrower\_first\_name”=\>“”, “subdivision”=\>“”, “block”=\>“”,  
“lot”=\>“”, “zip\_plus\_4”=\>“”, “address\_line\_1”=\>“dfsdfsdf”,  
“other\_information”=\>“”, “coborrower\_last\_name”=\>“”,  
“loan\_amount”=\>“”, “address\_line\_2”=\>“fddssdf”, “order\_state\_id”=\>“1”,  
“parcel”=\>“”, “builder\_phone”=\>“”, “state”=\>“”, “loan\_id\_1”=\>“”,  
“year\_built”=\>“”}, “action”=\>“create”, “controller”=\>“orders”}  
Order Columns (0.006320) SHOW FIELDS FROM orders  
Product Load (0.000749) SELECT \* FROM products WHERE (products.id  
IN (‘1’,‘2’,‘3’))  
SQL (0.000181) BEGIN  
Product Columns (0.001374) SHOW FIELDS FROM products  
SQL (0.000184) COMMIT  
LoanType Load (0.000704) SELECT \* FROM loan\_types WHERE  
(loan\_types.id IN (‘1’,‘2’,‘4’))  
SQL (0.000172) BEGIN  
LoanType Columns (0.001363) SHOW FIELDS FROM loan\_types  
SQL (0.000169) COMMIT  
SQL (0.000124) BEGIN  
SQL (0.000930) INSERT INTO orders (`city`,  
`coborrower_phone_number`, `created_on`, `loan_id_2`,  
`borrower_first_name`, `borrower_last_name`, `latitude`, `loan_id_3`,  
`zip`, `account_id`, `structure_type_id`, `updated_on`,  
`builder_name`, `coborrower_first_name`, `borrower_phone_number`,  
`address_line_1`, `block`, `lot`, `subdivision`, `zip_plus_4`,  
`address_line_2`, `coborrower_last_name`, `loan_amount`,  
`other_information`, `longitude`, `order_state_id`, `parcel`,  
`builder_phone`, `loan_id_1`, `state`, `year_built`) VALUES(‘’, NULL,  
‘2006-01-05 13:15:11’, ‘’, ‘’, ‘’, NULL, ‘’, ‘’, 10000, 1, ‘2006-01-05  
13:15:11’, ‘’, ‘’, NULL, ‘dfsdfsdf’, ‘’, ‘’, ‘’, ‘’, ‘fddssdf’, ‘’,  
‘’, ‘’, NULL, 1, ‘’, ‘’, ‘’, ‘’, NULL)  
loan\_types\_orders Columns (0.001317) SHOW FIELDS FROM  
loan\_types\_orders  
SQL (0.000496) INSERT INTO loan\_types\_orders (`order_id`,  
`loan_type_id`) VALUES (5, 1)  
loan\_types\_orders Columns (0.001122) SHOW FIELDS FROM  
loan\_types\_orders  
SQL (0.000471) INSERT INTO loan\_types\_orders (`order_id`,  
`loan_type_id`) VALUES (5, 2)  
loan\_types\_orders Columns (0.001477) SHOW FIELDS FROM  
loan\_types\_orders  
SQL (0.000456) INSERT INTO loan\_types\_orders (`order_id`,  
`loan_type_id`) VALUES (5, 4)  
SQL (0.000181) COMMIT

–

Keith Veleba  
keith (at) [veleba.net](http://veleba.net)

---

<div class="post-metadata">

**Author:** ![Keith\_Veleba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e68b1a/32.png) [@Keith\_Veleba](https://www.ruby-forum.com/u/Keith_Veleba)\
**Post date:** [February 27, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752/2 "2006-02-27T16:32:47Z")

</div>

Hi keith !

I have the same problem as you do !!! ☹  
Did you manage to resolve it ?  
If so, could you post it on the forum ?

Thanks a lot !!!

Julien

---

<div class="post-metadata">

**Author:** ![Keith\_Veleba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e68b1a/32.png) [@Keith\_Veleba](https://www.ruby-forum.com/u/Keith_Veleba)\
**Post date:** [February 27, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752/3 "2006-02-27T16:38:42Z")

</div>

Theres a known bug in active record where a model with 2 (or more) HABTM  
won’t save the 2nd one.

[http://wrath.rubyonrails.org/pipermail/rails/2005-December/005525.html](http://wrath.rubyonrails.org/pipermail/rails/2005-December/005525.html)

---

<div class="post-metadata">

**Author:** ![Keith\_Veleba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e68b1a/32.png) [@Keith\_Veleba](https://www.ruby-forum.com/u/Keith_Veleba)\
**Post date:** [February 28, 2006, 12:50am UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/51752/4 "2006-02-28T00:50:43Z")

</div>

On Monday 27 February 2006 10:38 am, Anthony G. wrote:

> Theres a known bug in active record where a model with 2 (or more) HABTM  
> won’t save the 2nd one.
> 
> [http://wrath.rubyonrails.org/pipermail/rails/2005-December/005525.html](http://wrath.rubyonrails.org/pipermail/rails/2005-December/005525.html)

This is fixed in edge rails.
