Update/update_attribute questions

Hi! In my view I’ve got a thing like this, that is a list with
checkboxes for visibility.

<% form_tag :action => ‘update_entries’ do%>
<% @entries.each do |e| -%>
<% @e=e -%>
<%= e.date -%>

<%= e.title -%>

<%= e.content -%>

<%= check_box( ‘e[]’,‘visible’,{},true,false) %>



<% end -%>
<%= submit_tag %>
<% end %>

When I send the form, this method is correctly executed:

def update_entries
Entry.update(params[:e].keys, params[:e].values);
redirect_to :action => ‘list’
end

Ok. But in Mongrel I see this:

Parameters: {“commit”=>“Save changes”, “action”=>“update_entries”,
“e”=>{“34”=>{“visible”=>“true”}, “35”=>{“visible”=>“false”},
“36”=>{“visible”=>“false”}, “37”=>{“visible”=>“false”},
“38”=>{“visible”=>“false”}, “39”=>{“visible”=>“false”}},
“controller”=>“admin”}

and then this one for each entry (even those I didn’t change):

Entry Load (0.001275) SELECT * FROM entries WHERE (entries.“id” = 34)
Entry Update (0.040439) UPDATE entries SET “visible” = ‘t’,
“content” = ‘test text’, “title” = ‘test title’, “entry_date” =
‘2007-10-15’ WHERE “id” = 34

So, I think that update is updating ALL the rows, and all the cols, not
only the rows/cols I’ve just changed. That’s not a good thing for me.
How can I fix this?

I’ve tried also this:

def update_entries
params[:e].each do |p|
row = Entry.find(p[0])
row.update_attribute(p[1].keys,p[1].keys)
end
redirect_to :action => ‘list’
end

I thought at least that update_attribute would write all the rows but
only in the ‘visible’ column, but it seems it does exactly the same
things of update, instead.

I really don’t understand why. Anyone can help me? :slight_smile:

On 2 Nov 2007, at 16:38, Luca R. wrote:

Ok. But in Mongrel I see this:

Parameters: {“commit”=>“Save changes”, “action”=>“update_entries”,
“e”=>{“34”=>{“visible”=>“true”}, “35”=>{“visible”=>“false”},
“36”=>{“visible”=>“false”}, “37”=>{“visible”=>“false”},
“38”=>{“visible”=>“false”}, “39”=>{“visible”=>“false”}},
“controller”=>“admin”}

and then this one for each entry (even those I didn’t change):

This is intentional: you get one parameter for every checkbox, even if
you didn’t change that check box (there’s some behind the scenes stuff
with a hidden field), which brings it inline with all the other input
types. If you use check_box_tag you won’t get this

How can I fix this?

Either use check_box_tag, or maybe use a hidden field ‘previous value’
so that your controller knows which parameters to ignore

I thought at least that update_attribute would write all the rows but
only in the ‘visible’ column, but it seems it does exactly the same
things of update, instead.

I really don’t understand why. Anyone can help me? :slight_smile:
That’s the just the way it is right now (for better or for worse).

Fred

Ok, now it’s all a bit clearer. Thank you very much!

Frederick C. wrote:

On 2 Nov 2007, at 16:38, Luca R. wrote:

Ok. But in Mongrel I see this:

Parameters: {“commit”=>“Save changes”, “action”=>“update_entries”,
“e”=>{“34”=>{“visible”=>“true”}, “35”=>{“visible”=>“false”},
“36”=>{“visible”=>“false”}, “37”=>{“visible”=>“false”},
“38”=>{“visible”=>“false”}, “39”=>{“visible”=>“false”}},
“controller”=>“admin”}

and then this one for each entry (even those I didn’t change):

This is intentional: you get one parameter for every checkbox, even if
you didn’t change that check box (there’s some behind the scenes stuff
with a hidden field), which brings it inline with all the other input
types. If you use check_box_tag you won’t get this

How can I fix this?

Either use check_box_tag, or maybe use a hidden field ‘previous value’
so that your controller knows which parameters to ignore

I thought at least that update_attribute would write all the rows but
only in the ‘visible’ column, but it seems it does exactly the same
things of update, instead.

I really don’t understand why. Anyone can help me? :slight_smile:
That’s the just the way it is right now (for better or for worse).

Fred