# How to update a list of Models 'automatically' in 1 action

**URL:** <https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899>\
**Category:** Rails\
**Created:** [July 31, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899 "2007-07-31T22:09:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 31, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899/1 "2007-07-31T22:09:31Z")

</div>

If I have a User table and a form that displays all my users with a  
Rank. I want to be able to rank my users and then hit ‘update’ and all  
users will be parsed by my action into User model obects and then  
persisted with their new rank to the DB.

Using “scaffold :user” in my controller works wonders for Single  
instance CRUD functionality of my User class, but can it be done with a  
list?

So basically I have this

def get\_users  
@user\_list = User.find\_all()  
end

In my views I’m doing.  
\<% form\_tag :action =\> ‘update\_users’ do %\>  
\<% for user in @user\_list %\>  
\<%-- display the user information here as a form --\>  
\<% end %\>  
\<% end %\>  
\<%= submit\_tag “Update Users” %\>

I would love to have the submit button (and associated action call)  
update ALL the users automatically without ME having to create a complex  
parser to break all the :params being passed into User objects and save  
them 1 by 1. (Through a loop of course).

I hope my description is clear… it’s crystal in my head. 😉

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 31, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899/2 "2007-07-31T23:26:40Z")

</div>

I am still relatively new to Rails so I am not sure if there is an  
automated way to do this, but if there isn’t, why would it be complex?  
This should work and isn’t too complex…

In Controller:  
def update\_users  
@user\_list = User.find(params[:user].keys)  
for user in @user\_list  
user.rank = params[:user][user.id][:rank]  
user.save  
end  
end

In View:  
\<% for @user in @user\_list %\>  
\<%= text\_field ‘user[]’, ‘rank’ %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 2, 2007, 3:27am UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899/3 "2007-08-02T03:27:35Z")

</div>

> In Controller:  
> def update\_users  
> @user\_list = User.find(params[:user].keys)  
> for user in @user\_list  
> user.rank = params[:user][user.id][:rank]  
> user.save  
> end  
> end
> 
> In View:  
> \<% for @user in @user\_list %\>  
> \<%= text\_field ‘user[]’, ‘rank’ %\>  
> \<% end %\>

I hate to reply to my own post, but I just found a way to do it with one  
line. The view code I wrote above is still applicable, but in the  
controller you can put:

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

I found this in the book Agile Web D. with Rails, 2nd ed, p499.

There doesn’t seem to be a good way to catch errors with this method,  
though.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 2, 2007, 6:47am UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899/4 "2007-08-02T06:47:21Z")

</div>

Kip wrote:

> Dondi,
> 
> Perhaps this might help (with lots of error checking)

The OP wanted to update the whole list of users as simple as possible. I  
do not know if they needed error checking or not, so I wanted to point  
out that the one-liner I found won’t catch errors (it returns the user  
objects even if they failed validation and could not be saved).

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 2, 2007, 4:04am UTC](https://www.ruby-forum.com/t/how-to-update-a-list-of-models-automatically-in-1-action/110899/5 "2007-08-02T04:04:52Z")

</div>

Dondi,

Perhaps this might help (with lots of error checking)  
if user = User.find(params[:user][:id]) then  
if user.update\_attributes(params[:user].keys,  
params[:user].values) then  
if user.save then  
puts “All updated OK”  
redirect or something  
else  
puts “Save failed”  
end  
else  
puts “Validation failed”  
end  
else  
puts "Couldn’t find the user in the database  
end  
render

Cheers, --Kip

On Aug 2, 9:27 am, Dondi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
