# Update multiple records in the same table

**URL:** <https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621>\
**Category:** Rails\
**Created:** [September 3, 2007, 12:37am UTC](https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621 "2007-09-03T00:37:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [September 3, 2007, 12:37am UTC](https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621/1 "2007-09-03T00:37:47Z")

</div>

Hey,

Sorry to ask this, but I’m really confused!

I have an app to handle guest lists. When a user wants to edit a list, I  
create a one form with fields for each guest to make it easier to update  
all in one go. Like this…

Guest 1 | [name] [company] [email]

Guest 2 | [name] [company] [email]

Guest 3 | [name] [company] [email]

SUBMIT

How do I update them all with one form submission?

Thanks

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [September 3, 2007, 2:22am UTC](https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621/2 "2007-09-03T02:22:10Z")

</div>

Hi Scott,

I’m going to haml here because rhtml sucks hard to code:

edit\_multiple.haml:  
= form\_tag :controller =\> ‘guests’, :action =\> ‘update\_multiple’  
%table  
= render :partial =\> ‘form’, :collection =\> @guests

\_form.haml:  
%tr  
%td= input\_tag “guest[#{guest.id}][name]”, guest.name  
%td= input\_tag “guest[#{guest.id}][company]”, guest.company  
…

guests\_controller.rb:  
class GuestsController \< ApplicationController

# RESTful routes or:

verify :method =\> :post, :params =\> :guest, :only =\> :update\_multiple

def update\_multiple  
guest\_ids = params[:guest].keys

```
# better off by placing this in the model...
Guest.transaction do
  guest_ids.each { |guest_id| Guest.update guest_id, 

```

params[guest\_id] }  
end  
end

end

Untested!

Regards  
Florian

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [September 3, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621/3 "2007-09-03T20:46:58Z")

</div>

Hey,

Thanks for that. I found this code which seems to work:

def update\_guestlist

@params[:guest].each do |g, guest|  
guest\_line = Guest.find(g)  
guest\_line.update\_attributes(guest)  
end

redirect\_to :controller =\> ‘groups’

end

But is there anyway of ensuring that all of the data has been saved  
before redirecting?

Thanks!

Ps - Thanks for the HAML tip. I have never heard of it but it seems to  
take a lot of the work out of coding the view!

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [September 4, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/update-multiple-records-in-the-same-table/113621/4 "2007-09-04T12:18:37Z")

</div>

Scott H. wrote:

> But is there anyway of ensuring that all of the data has been saved  
> before redirecting?

You will want to look into transactions. Since the rails wiki is down,  
here is the cached Google page.

[http://64.233.167.104/search?q=cache:rGXAaODr8moJ:wiki.rubyonrails.org/rails/show/HowToUseTransactions+rails+transactions&hl=en&ct=clnk&cd=1&gl=au](http://64.233.167.104/search?q=cache:rGXAaODr8moJ:wiki.rubyonrails.org/rails/show/HowToUseTransactions+rails+transactions&hl=en&ct=clnk&cd=1&gl=au)
