Forum: Ruby on Rails ActionRecord: how to update many records in one statement

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex R. (Guest)
on 2006-01-23 19:36
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:
-------------------------------
<input type="hidden" name="interest[<%= interest.id %>][id]" value="<%=
interest.id %>" />
<input type="text" id="interest_<%= interest.id %>_name"
name="interest[<%= interest.id %>][name]" value="<%= interest.name %>"
/>
<textarea cols="40" rows="2" id="interest_<%= interest.id
%>_description" name="interest[<%= interest.id %>][description]">
      	<%= interest.description %>
</textarea>

-------------------------------
This is edit.rhtml template:
-------------------------------
<%= start_form_tag :action=> "edit" %>
	<% for interest in @editintersts %>
		<%= render :partial => "interst", :locals => { :interest => interest }
%>
	<% end %>
 <input type="submit" value="Edit;" class="primary" />
<%= 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
r00by n00by (Guest)
on 2006-01-24 01:10
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.
Alexey Rudnitskiy (Guest)
on 2006-01-26 22:34
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:
> -------------------------------
> <input type="hidden" name="interest[<%= interest.id %>][id]" value="<%=
> interest.id %>" />
> <input type="text" id="interest_<%= interest.id %>_name"
> name="interest[<%= interest.id %>][name]" value="<%= interest.name %>"
> />
> <textarea cols="40" rows="2" id="interest_<%= interest.id
> %>_description" name="interest[<%= interest.id %>][description]">
>       	<%= interest.description %>
> </textarea>
>
> -------------------------------
> This is edit.rhtml template:
> -------------------------------
> <%= start_form_tag :action=> "edit" %>
> 	<% for interest in @editintersts %>
> 		<%= render :partial => "interst", :locals => { :interest => interest }
> %>
> 	<% end %>
>  <input type="submit" value="Edit;" class="primary" />
> <%= 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
Jonathan V. (Guest)
on 2006-01-27 00:36
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
Juicy (Guest)
on 2006-01-27 02:41
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...
This topic is locked and can not be replied to.