# Updating multiple records from a hash

**URL:** <https://www.ruby-forum.com/t/updating-multiple-records-from-a-hash/106176>\
**Category:** Rails\
**Created:** [June 7, 2007, 3:49am UTC](https://www.ruby-forum.com/t/updating-multiple-records-from-a-hash/106176 "2007-06-07T03:49:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 7, 2007, 3:49am UTC](https://www.ruby-forum.com/t/updating-multiple-records-from-a-hash/106176/1 "2007-06-07T03:49:56Z")

</div>

I’m trying to figure out how to update multiple records in one method  
from a hash sent by a view, and unfortunately, i’m dead stuck.

Here’s what i’m sending:

\<% form\_tag :controller =\> ‘todoitems’, :action =\> ‘toggle\_todo’, :id =\>  
@id do -%\>  
\<% if @todoitems then @todoitems.each do |t| -%\>

  
\<%= check\_box(“to\_toggle”, t.id, {:checked =\> t.done}, TRUE,  
FALSE) %\>  
\<%= t.description %\>  

  
\<% end -%\>  
\<% end -%\>  
\<%= submit\_tag ‘Update’ %\>  
\<% end %\>

This is what gets sent:

Parameters: {“commit”=\>“Update”, “to\_toggle”=\>{“6”=\>“true”,  
“7”=\>“false”, “8”=\>“false”, “9”=\>“true”}, “action”=\>“toggle\_todo”,  
“id”=\>“8”, “controller”=\>“todoitems”}

And this is the controller def that _should_ modify the records for me,  
but it doesn’t do anything. I commented it out so people can see what  
I’m trying to do:

def toggle\_todo  
#takes the hash out of params and assigns it to @toggle  
@toggle = params[:to\_toggle]  
if @toggle  
#iterate over the hash  
@toggle.each do |id, value|  
#find the record  
@todoitem = Todoitem.find\_by\_id(id)  
#change the record’s “done” column to the right value  
@todoitem.done = value  
end  
end  
#redirect  
redirect\_to :controller =\> ‘showproject’, :action =\> ‘show’, :id =\>  
params[:id]  
end

Each todoitem has a “done” column that’s a boolean to show if it’s done  
or not.

Anybody see what I’m missing? Thanks!

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 7, 2007, 4:27am UTC](https://www.ruby-forum.com/t/updating-multiple-records-from-a-hash/106176/2 "2007-06-07T04:27:08Z")

</div>

Ignore this thread. The server burped when I was submitting it, and I  
ended up sending it twice. Sorry!
