# HABTM problem not saving all associations

**URL:** <https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/94593>\
**Category:** Rails\
**Created:** [January 6, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/94593 "2006-01-06T21:04:00Z")\
**Posts on this page:** 3\
**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, 9:04pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/94593/1 "2006-01-06T21:04:00Z")

</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:** [January 6, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/94593/2 "2006-01-06T21:09:37Z")

</div>

This might explain your problem…

[http://dev.rubyonrails.org/ticket/3213](http://dev.rubyonrails.org/ticket/3213)

---

<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, 9:31pm UTC](https://www.ruby-forum.com/t/habtm-problem-not-saving-all-associations/94593/3 "2006-01-06T21:31:04Z")

</div>

That’s it! Thanks for your help, I really appreciate it.

On 1/6/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–

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