Alex Rudnitski
2006-Jan-23 17:36 UTC
[Rails] ActionRecord: how to update many records in one statement
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
--
Posted via http://www.ruby-forum.com/.
r00by n00by
2006-Jan-23 23:10 UTC
[Rails] Re: ActionRecord: how to update many records in one statemen
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.
--
Posted via http://www.ruby-forum.com/.
Alexey Rudnitskiy
2006-Jan-26 20:34 UTC
[Rails] Re: ActionRecord: how to update many records in one statemen
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 > endI 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 -- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-Jan-26 22:36 UTC
[Rails] Re: ActionRecord: how to update many records in one statemen
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-- Posted via http://www.ruby-forum.com/.
Juicy
2006-Jan-27 00:41 UTC
[Rails] Re: ActionRecord: how to update many records in one statemen
Jonathan Viney 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... -- Posted via http://www.ruby-forum.com/.