# Updating records from a hash

**URL:** <https://www.ruby-forum.com/t/updating-records-from-a-hash/106174>\
**Category:** Rails\
**Created:** [June 7, 2007, 3:45am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174 "2007-06-07T03:45:24Z")\
**Posts on this page:** 8\
**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:45am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/1 "2007-06-07T03:45:24Z")

</div>

I’m trying to figure out how to update multiple records in one method  
from a hash sent by a view, and i’m 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, 3:55am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/2 "2007-06-07T03:55:26Z")

</div>

add this in ur loop:

@todoitem.done = value  
@todoitem.save

You forgot to save the value that you are changing.

---

<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:28am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/3 "2007-06-07T04:28:52Z")

</div>

Whoops. Didn’t know I had to do that, actually.

Now I do!

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:45am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/4 "2007-06-07T04:45:01Z")

</div>

One thing but, you might want to wrap that in a transaction, if one of  
your loops fails, you’ll end up in a strange database state.

Cam

On Jun 7, 12:28 pm, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:07pm UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/5 "2007-06-07T15:07:23Z")

</div>

Josh,  
I wrote into the [http://railsforum.com](http://railsforum.com) yesterday and heard no reply on  
a topic related to this.  
When Rails programmers’ deploy a ‘naked’ application to the web before  
it has any user activity, it is VERY unattractive to search engines. I  
thought it would be wise to embed one’s ‘keywords’ into a few text  
fields while generating ‘placebo’ records upon initial deployment.  
I’d like to write a simple record generation routine that reads from a  
KEYWORD hash and at various intervals substitutes these keywords with  
a variable.  
Could/would you point me in the right direction of getting started  
with this.  
Thank you,  
David

On Jun 6, 11:19 pm, Joshua B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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, 6:19am UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/6 "2007-06-07T06:19:29Z")

</div>

Sean C. wrote:

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

…snip…

> 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

Why not skip iterating over the hash and just use the  
ActiveRecord::Base#update method? Your code would look a lot neater:

def toggle\_todo  
@toggle = params[:to\_toggle]  
if @toggle  
Todoitem.update(@toggle.keys, @toggle.values)  
end  
redirect\_to :controller =\> ‘showproject’, :action =\> ‘show’, :id =\>  
params[:id]  
end

Cheers,  
Josh

---

<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 8, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/7 "2007-06-08T14:44:23Z")

</div>

Josh,  
I’ve thorough searched the web for any Rails code relating to this and  
find none. To me, it’s just another example of how Rails programmers’  
are decoupled from the world of business.  
I’d expire records based on time. These ‘keyword’ rich records would  
be groomed out after reaching a certain age.  
If I could see a chunck of code that read an input record (as you  
describe in AWD, it would then run a ‘block’ that would run through a  
‘hash’ and replaced words like REPLACEME with the next ‘keyword’ in  
the hash.  
I’m making great strides in developing the ‘mother’ of all web  
applications, but am religated to find and copy code as I don’t have  
the intellect/experience to create my own. Obviously, I’m the business  
side of this, too.  
I am happy to share with the group, whatever I come up with.  
David

On Jun 7, 10:59 am, Joshua B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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, 5:59pm UTC](https://www.ruby-forum.com/t/updating-records-from-a-hash/106174/8 "2007-06-07T17:59:22Z")

</div>

BraveDave wrote:

> Josh,  
> I wrote into the [http://railsforum.com](http://railsforum.com) yesterday and heard no reply on  
> a topic related to this.  
> When Rails programmers’ deploy a ‘naked’ application to the web before  
> it has any user activity, it is VERY unattractive to search engines. I  
> thought it would be wise to embed one’s ‘keywords’ into a few text  
> fields while generating ‘placebo’ records upon initial deployment.  
> I’d like to write a simple record generation routine that reads from a  
> KEYWORD hash and at various intervals substitutes these keywords with  
> a variable.  
> Could/would you point me in the right direction of getting started  
> with this.  
> Thank you,  
> David

Hmm…interesting. Well, I can definitely see why something like this  
might be useful. I guess I’m just not sure exactly how you’re looking to  
implement this. Do you want temporary records that will eventually be  
replaced as your web app grows? I think the best way to import  
test/temporary records is via a migration. The Pragmatic Programmers do  
this in _Agile Web D. with Rails_ (which, if you don’t have  
already, is an excellent resource). You can find the example code at  
[http://www.pragmaticprogrammer.com/titles/rails/code.html](http://www.pragmaticprogrammer.com/titles/rails/code.html). You could  
then add a “temp\_item” column to your model, and every time a real  
record comes in, remove one of the temp items until they’re all gone.  
Not sure if this is what you’re looking for…

-Josh
