Updating records from a hash

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!

add this in ur loop:

@todoitem.done = value
@todoitem.save

You forgot to save the value that you are changing.

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

Now I do!

Thanks!

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. [email protected]

Josh,
I wrote into the 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. [email protected]

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

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. [email protected]

BraveDave wrote:

Josh,
I wrote into the 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. 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