# HABTM updating/saving behaviour

**URL:** <https://www.ruby-forum.com/t/habtm-updating-saving-behaviour/104372>\
**Category:** Rails\
**Created:** [May 17, 2007, 9:27am UTC](https://www.ruby-forum.com/t/habtm-updating-saving-behaviour/104372 "2007-05-17T09:27:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Morandini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@Andrea\_Morandini](https://www.ruby-forum.com/u/Andrea_Morandini)\
**Post date:** [May 17, 2007, 9:27am UTC](https://www.ruby-forum.com/t/habtm-updating-saving-behaviour/104372/1 "2007-05-17T09:27:06Z")

</div>

Newbie question (I guess).

I have a User whom can have some Preference. So the scracth model is:

class User \< ActiveRecord:Base  
[…]  
has\_and\_belongs\_to\_many :preferences  
[…]  
end

class Preference \< ActiveRecord:Base  
[…]  
has\_and\_belongs\_to\_many : users  
[…]  
end

In the migration I have created a table preferences\_users with primary  
keys  
preference\_id and user\_id

In the view the choice of which preferences are connected to and user is  
performed by means of a checkbox sets.

In my user\_controller scalffold generated actions “create” and “update”  
I  
added the following lines:

[…]  
params[:preferences].each { |k,v|  
if v == 1 then #if checkbox is checked  
preferences \<\< Preference.find(k)  
end  
}  
[…]  
the user is saved or updated

So I have two question:

1. Is there a better way to perform that check, ot any helper that  
already  
do that task?
2. Actually in the join table preferences\_users nothing is saved. I have  
to  
force the saving ?
