ActionRecord: how to update many records in one statement


#1

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


#2

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.


#3

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


#4

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


#5

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…