my query look like this; "update table_a set field_1=''ABCD'' where field_2=''A'';" it doesn''t work with this: @arr = table_a.find(["field_1 = ? ", "ABCD"]) for item in arr item.field_2 = "A" item.save end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@arr = table_a.find(:all, :conditions => ''field_1 = ?'', "ABCD'']) then run your loop... although it should be for item in @arr ( not just arr, but I assumed that was a typo ) I was guessing at what you wanted, your question isn''t too clear. Cheers sw0rdfish On May 27, 2:59 pm, Dev Xpert <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> my query look like this; > > "update table_a set field_1=''ABCD'' where field_2=''A'';" > > it doesn''t work with this: > > @arr = table_a.find(["field_1 = ? ", "ABCD"]) > > for item in arr > item.field_2 = "A" > item.save > end > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
actually I want to update multiple rows with value "A" sw0rdfish wrote:> @arr = table_a.find(:all, :conditions => ''field_1 = ?'', "ABCD'']) > > then run your loop... although it should be > > for item in @arr ( not just arr, but I assumed that was a typo ) > > I was guessing at what you wanted, your question isn''t too clear. > > Cheers > sw0rdfish > > On May 27, 2:59�pm, Dev Xpert <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
then my example should work no? IF not what errors do you get? On May 27, 3:08 pm, Dev Xpert <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> actually I want to update multiple rows with value "A" > > sw0rdfish wrote: > > @arr = table_a.find(:all, :conditions => ''field_1 = ?'', "ABCD'']) > > > then run your loop... although it should be > > > for item in @arr ( not just arr, but I assumed that was a typo ) > > > I was guessing at what you wanted, your question isn''t too clear. > > > Cheers > > sw0rdfish > > > On May 27, 2:59�pm, Dev Xpert <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dev Xpert wrote:> my query look like this; > > "update table_a set field_1=''ABCD'' where field_2=''A'';" > > > it doesn''t work with this: > > @arr = table_a.find(["field_1 = ? ", "ABCD"])That''s got it all bass-ackwards, you want to find based on field_2 - your where clause, and update field_1 - the SET statement.> > for item in arr > item.field_2 = "A" > item.save > end# brute force, being obvious about what we do @table_as = Table_a.find(:all, :conditions => ["field_2 = ?", ''A'']) for table_a in @table_as @table_a = Table_a.find(table_a) @table_a.field_1 = ''ABCD'' @table_a.save end I''m sure there''s a simpler way statement-wise to do it, but that''s off the cuff. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yes... there is a much simpler way... Model.update_all accepts conditions. * http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001383 Good luck, Robby On Tue, May 27, 2008 at 2:03 PM, Ar Chron <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Dev Xpert wrote: >> my query look like this; >> >> "update table_a set field_1=''ABCD'' where field_2=''A'';" >> >> >> it doesn''t work with this: >> >> @arr = table_a.find(["field_1 = ? ", "ABCD"]) > > That''s got it all bass-ackwards, you want to find based on field_2 - > your where clause, and update field_1 - the SET statement. > >> >> for item in arr >> item.field_2 = "A" >> item.save >> end > > # brute force, being obvious about what we do > @table_as = Table_a.find(:all, :conditions => ["field_2 = ?", ''A'']) > for table_a in @table_as > @table_a = Table_a.find(table_a) > @table_a.field_1 = ''ABCD'' > @table_a.save > end > > I''m sure there''s a simpler way statement-wise to do it, but that''s off > the cuff. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Robby Russell Founder and Executive Director PLANET ARGON, LLC Design, Development, and Hosting with Ruby on Rails http://www.planetargon.com/ http://www.robbyonrails.com/ aim: planetargon +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4068 [fax] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---