# Update/update\_attribute questions

**URL:** <https://www.ruby-forum.com/t/update-update-attribute-questions/119057>\
**Category:** Rails\
**Created:** [November 2, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/update-update-attribute-questions/119057 "2007-11-02T17:38:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/update-update-attribute-questions/119057/1 "2007-11-02T17:38:39Z")

</div>

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? 🙂

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/update-update-attribute-questions/119057/2 "2007-11-02T18:10:01Z")

</div>

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? 🙂  
> That’s the just the way it is right now (for better or for worse).

Fred

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/update-update-attribute-questions/119057/3 "2007-11-02T18:17:55Z")

</div>

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? 🙂  
> > That’s the just the way it is right now (for better or for worse).
> 
> Fred
