# ActionRecord: how to update many records in one statement

**URL:** <https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290>\
**Category:** Rails\
**Created:** [January 23, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290 "2006-01-23T18:36:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@wiz](https://www.ruby-forum.com/u/wiz)\
**Post date:** [January 23, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290/1 "2006-01-23T18:36:20Z")

</div>

Hello,

I am trying to edit and update all records of table â??interstsâ?? (id,  
name, description) on one page and has no good idea how to do it right,  
when updating one record per page itâ??s ok The question is: what  
statement instead Interest.update\_all(params[:interest]) in controller  
need to use to successfully update the database table by data passing to  
controller in parameter â??interestâ??? Looked for solution in other places  
didnâ??t make any results, please help if you know how to do it.

Thanks a lot.

Sorry for such ugly piece of code, but I cannot explain the solution  
that I trying better then it.

* * *

## This is partial template (\_interest.rhtml) for representing one record from database table:

\<%= interest.description %\>

* * *

## This is edit.rhtml template:

\<%= start\_form\_tag :action=\> “edit” %\>  
\<% for interest in @editintersts %\>  
\<%= render :partial =\> “interst”, :locals =\> { :interest =\> interest }  
%\>  
\<% end %\>  
  
\<%= end\_form\_tag %\>

* * *

## This is request parameters passed to controller:

“interest”=\>  
{  
“1”=\>{“1”=\>{“name”=\>“Music”, “id”=\>“1”, “description”=\>" I like to  
listen to the music"}, “2”=\>{“name”=\>“Games”, “id”=\>“2”,  
“description”=\>" I like to play the games"}, “3”=\>{“name”=\>“Sport”,  
“id”=\>“3”, “description”=\>“I like to go in for sport”}  
}

* * *

## This is the controller

def edit  
@editintersts = Interest.find\_all  
if @request.post?  
Interest.update\_all(params[:interest]) # **The problem is here**  
end  
end

---

<div class="post-metadata">

**Author:** ![wiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@wiz](https://www.ruby-forum.com/u/wiz)\
**Post date:** [January 24, 2006, 12:10am UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290/2 "2006-01-24T00:10:09Z")

</div>

Shoudn’t interset be  
“interest”=\>  
{  
“1”=\>{“name”=\>“Music”, “id”=\>“1”, “description”=\>" I like to  
listen to the music"},  
“2”=\>{“name”=\>“Games”, “id”=\>“2”,  
“description”=\>" I like to play the games"},  
“3”=\>{“name”=\>“Sport”,  
“id”=\>“3”, “description”=\>“I like to go in for sport”  
}

?

As far as I understand, update\_all is good when you want to update a  
number of records with the same value, for example -  
Interest.update\_all(“name = Music”) updates the “name” attribute of all  
the records in “interests” with the value “Music”. This is not what you  
need.  
I think you need to use a loop, maybe like this:

def edit  
@editintersts = Interest.find\_all  
if @request.post?  
for attribs in params[:interest]  
interest = Interest.update(attribs[‘id’],attribs)  
end  
end  
end

I didn’t try it myself, so it could be wrong.

---

<div class="post-metadata">

**Author:** ![wiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@wiz](https://www.ruby-forum.com/u/wiz)\
**Post date:** [January 26, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290/3 "2006-01-26T21:34:05Z")

</div>

Alex Rudnitski wrote:

> Hello,
> 
> I am trying to edit and update all records of table â??interstsâ?? (id,  
> name, description) on one page and has no good idea how to do it right,  
> when updating one record per page itâ??s ok The question is: what  
> statement instead Interest.update\_all(params[:interest]) in controller  
> need to use to successfully update the database table by data passing to  
> controller in parameter â??interestâ??? Looked for solution in other places  
> didnâ??t make any results, please help if you know how to do it.
> 
> Thanks a lot.
> 
> Sorry for such ugly piece of code, but I cannot explain the solution  
> that I trying better then it.
> 
> * * *
> 
> ## This is partial template (\_interest.rhtml) for representing one record from database table:
> 
> \<%= interest.description %\>
> 
> * * *
> 
> ## This is edit.rhtml template:
> 
> \<%= start\_form\_tag :action=\> “edit” %\>  
> \<% for interest in @editintersts %\>  
> \<%= render :partial =\> “interst”, :locals =\> { :interest =\> interest }  
> %\>  
> \<% end %\>  
>   
> \<%= end\_form\_tag %\>
> 
> * * *
> 
> ## This is request parameters passed to controller:
> 
> “interest”=\>  
> {  
> “1”=\>{“1”=\>{“name”=\>“Music”, “id”=\>“1”, “description”=\>" I like to  
> listen to the music"}, “2”=\>{“name”=\>“Games”, “id”=\>“2”,  
> “description”=\>" I like to play the games"}, “3”=\>{“name”=\>“Sport”,  
> “id”=\>“3”, “description”=\>“I like to go in for sport”}  
> }
> 
> * * *
> 
> ## This is the controller
> 
> def edit  
> @editintersts = Interest.find\_all  
> if @request.post?  
> Interest.update\_all(params[:interest]) # **The problem is here**  
> end  
> end

I have been modified the method to:

def edit  
if @request.post?  
records = @params[:interest]  
for n, record in records  
rec = Interest.find(record[“id”])  
rec.update\_attributes(record)  
end  
end  
@editintersts = Interest.find(:all, :order =\> “id ASC”)  
end

---

<div class="post-metadata">

**Author:** ![wiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@wiz](https://www.ruby-forum.com/u/wiz)\
**Post date:** [January 26, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290/4 "2006-01-26T23:36:04Z")

</div>

You can actually combine find and update\_attributes into one line

@params[:interest].each do |key, record|  
Interest.update(record[‘id’], record)  
end

-Jonny.

> I have been modified the method to:
> 
> def edit  
> if @request.post?  
> records = @params[:interest]  
> for n, record in records  
> rec = Interest.find(record[“id”])  
> rec.update\_attributes(record)  
> end  
> end  
> @editintersts = Interest.find(:all, :order =\> “id ASC”)  
> end

---

<div class="post-metadata">

**Author:** ![wiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@wiz](https://www.ruby-forum.com/u/wiz)\
**Post date:** [January 27, 2006, 1:41am UTC](https://www.ruby-forum.com/t/actionrecord-how-to-update-many-records-in-one-statement/96290/5 "2006-01-27T01:41:12Z")

</div>

Jonathan V. wrote:

> You can actually combine find and update\_attributes into one line
> 
> @params[:interest].each do |key, record|  
> Interest.update(record[‘id’], record)  
> end
> 
> -Jonny.

Does this build one query? Or does it talk to the database on every  
iteration? The latter would be much slower, I’d imagine.

-B…
