Basically what I''m trying to do is get the sum of 2 columns and save those results in another. I guess I''m looking to do something like this: a = Product.find(:all) b = Product.sum("test + test2", :group => "id") a.test3 = b a.save Obviously that doesn''t work, but I''m trying to achieve something like that. Any suggestions? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-16 12:19 UTC
Re: Saving results of sum method into another column?
On 16 Jan 2009, at 10:19, Herman wrote:> > Basically what I''m trying to do is get the sum of 2 columns and save > those results in another. I guess I''m looking to do something like > this: > a = Product.find(:all) > b = Product.sum("test + test2", :group => "id") > a.test3 = b > a.save > > Obviously that doesn''t work, but I''m trying to achieve something like > that. Any suggestions? >Product.update "test3 = test+test2" would probably do it. Fred> >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That didn''t work. I received: ArgumentError: wrong number of arguments (1 for 2) I tried something like this and it also didn''t work: Product.update_all("test3", :sum => "test"+"test2", :group => "id") I''d imagine I''m not that far off, but I can''t figure it out. On Jan 16, 6:19 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 Jan 2009, at 10:19, Herman wrote: > > > > > Basically what I''m trying to do is get the sum of 2 columns and save > > those results in another. I guess I''m looking to do something like > > this: > > a = Product.find(:all) > > b = Product.sum("test + test2", :group => "id") > > a.test3 = b > > a.save > > > Obviously that doesn''t work, but I''m trying to achieve something like > > that. Any suggestions? > > Product.update "test3 = test+test2" would probably do it. > > Fred > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-16 18:41 UTC
Re: Saving results of sum method into another column?
On 16 Jan 2009, at 16:34, Herman wrote:> > That didn''t work. I received: ArgumentError: wrong number of arguments > (1 for 2) > > I tried something like this and it also didn''t work: > Product.update_all("test3", :sum => "test"+"test2", :group => "id") >oops, typo, should be Product.update_all "test3 = test+test2"> I''d imagine I''m not that far off, but I can''t figure it out. > > On Jan 16, 6:19 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 16 Jan 2009, at 10:19, Herman wrote: >> >> >> >>> Basically what I''m trying to do is get the sum of 2 columns and save >>> those results in another. I guess I''m looking to do something like >>> this: >>> a = Product.find(:all) >>> b = Product.sum("test + test2", :group => "id") >>> a.test3 = b >>> a.save >> >>> Obviously that doesn''t work, but I''m trying to achieve something >>> like >>> that. Any suggestions? >> >> Product.update "test3 = test+test2" would probably do it. >> >> Fred >> >> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Works great. Thank you so much! On Jan 16, 12:41 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 Jan 2009, at 16:34, Herman wrote: > > > > > That didn''t work. I received: ArgumentError: wrong number of arguments > > (1 for 2) > > > I tried something like this and it also didn''t work: > > Product.update_all("test3", :sum => "test"+"test2", :group => "id") > > oops, typo, should be Product.update_all "test3 = test+test2" > > > I''d imagine I''m not that far off, but I can''t figure it out. > > > On Jan 16, 6:19 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> On 16 Jan 2009, at 10:19, Herman wrote: > > >>> Basically what I''m trying to do is get the sum of 2 columns and save > >>> those results in another. I guess I''m looking to do something like > >>> this: > >>> a = Product.find(:all) > >>> b = Product.sum("test + test2", :group => "id") > >>> a.test3 = b > >>> a.save > > >>> Obviously that doesn''t work, but I''m trying to achieve something > >>> like > >>> that. Any suggestions? > > >> Product.update "test3 = test+test2" would probably do it. > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Bartlett
2009-Jan-17 20:36 UTC
Re: Saving results of sum method into another column?
Just to give another perspective... Do you really need to save the sum? Could it be calculated whenever you need it? If you save the sum you need to be confident that nothing will cause your data to get out of sync, such as some update to ''test'' that does not update the sum. If you decide you do want to save the sum, could the database management system handle it for you? E.g. in PostgreSQL you can define triggers - in this case a trigger could update the sum for you no matter how ''test'' and ''test2'' are updated. You''d then have much greater faith in the reliability of each sum. On Jan 16, 11:19 pm, Herman <bumpmap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Basically what I''m trying to do is get the sum of 2 columns and save > those results in another. I guess I''m looking to do something like > this: > a = Product.find(:all) > b = Product.sum("test + test2", :group => "id") > a.test3 = b > a.save > > Obviously that doesn''t work, but I''m trying to achieve something like > that. Any suggestions?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---