# Update attributes for multiple objects

**URL:** <https://www.ruby-forum.com/t/update-attributes-for-multiple-objects/66467>\
**Category:** Rails\
**Created:** [August 26, 2006, 10:53am UTC](https://www.ruby-forum.com/t/update-attributes-for-multiple-objects/66467 "2006-08-26T10:53:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_von\_Rohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Matthias\_von\_Rohr](https://www.ruby-forum.com/u/Matthias_von_Rohr)\
**Post date:** [August 26, 2006, 10:53am UTC](https://www.ruby-forum.com/t/update-attributes-for-multiple-objects/66467/1 "2006-08-26T10:53:49Z")

</div>

Hi everyone,

I’ve been thinking and googling about this for the whole afternoon and I  
dont find the solution, but I know there must be an easy way (after all  
this  
is rails):

I have a page where user objects are listed (e.g. customers) and they  
have  
several attributes (like name, e-mail etc.) and a special attribute  
called  
contacted (which can be true or false depending on if this customer has  
been  
contacted by the sales people or not)

No big deal I thought, just write two controller methods ‘set\_contacted’  
and  
‘unset\_contacted’ and we are done by passing over the IDs of the  
corresponding objects. But then there came in two more attributes and I  
suddenly end up with 6 controller methods which feels horrible wrong to  
me  
(Not even to mention the DRY principle violation).

What I would like to do is the following using checkboxes and a form for  
the  
whole table and a submit button in order to save the changes made to the  
user objects

User | contacted  
user1 | [x]  
user2 | []  
user3 | []

[Save changes]

You sometimes see a similar behaviour in Webmail clients where you can  
check  
serveral e-mails at once and then hit a delete button.

Any sort of help is appreciated! Have a nice week-end

Matt

P.S is it possible that submitting topics from the web interface of  
google  
talks does not work?

* * *

[http://www.matthiasvonrohr.ch](http://www.matthiasvonrohr.ch)

---

<div class="post-metadata">

**Author:** ![Matthias\_von\_Rohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Matthias\_von\_Rohr](https://www.ruby-forum.com/u/Matthias_von_Rohr)\
**Post date:** [August 26, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/update-attributes-for-multiple-objects/66467/2 "2006-08-26T13:22:26Z")

</div>

Matthias von Rohr wrote:

> What I would like to do is the following using checkboxes and a form for the  
> whole table and a submit button in order to save the changes made to the  
> user objects
> 
> User | contacted  
> user1 | [x]  
> user2 | []  
> user3 | []
> 
> [Save changes]

If you have multiple instances of the same model in a form, you can  
refer to them like this:

\<% for @user in @users -%\>  
\<%= check\_box(“user[]”, “contacted”) %\>  
\<%= check\_box(“user[]”, “replied”) %\>  
\<% end -%\>

In the controller, you’ll find that params[:user] is a hash of hashes  
that looks something like this:

{ 1 =\> {“contacted” =\> 1, “replied” =\> 1},  
2 =\> {“contacted” =\> 1, “replied” =\> 0},  
3 =\> {“contacted” =\> 0, “replied” =\> 0} }

where 1, 2 and 3 are the ids of the users.

You can then use that to update the user objects in one go:

User.update(params[:user].keys, params[:user].values)

Chris

---

<div class="post-metadata">

**Author:** ![Matthias\_von\_Rohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Matthias\_von\_Rohr](https://www.ruby-forum.com/u/Matthias_von_Rohr)\
**Post date:** [August 26, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/update-attributes-for-multiple-objects/66467/3 "2006-08-26T16:34:49Z")

</div>

Thanks a lot! Works like a charm!
