Update multiple records in the same table

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

Scott Holland 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