Has anyone come across documentation on how to execute an update_by_sql function on an ActiveRecord object? This would be similar to X.find_by_sql. Any help would be greatly appreciated! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
peri wrote:> Has anyone come across documentation on how to execute an > update_by_sql function on an ActiveRecord object? > This would be similar to X.find_by_sql.There is no such method: http://api.rubyonrails.org/classes/ActiveRecord/Base.html Why would you want to have update_by_sql? Lutz -- 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 -~----------~----~----~----~------~----~------~--~---
On Oct 30, 2007 12:42 PM, Lutz Horn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > peri wrote: > > Has anyone come across documentation on how to execute an > > update_by_sql function on an ActiveRecord object? > > This would be similar to X.find_by_sql. > > There is no such method: > http://api.rubyonrails.org/classes/ActiveRecord/Base.html > > Why would you want to have update_by_sql? > > Lutz > -- > Posted via http://www.ruby-forum.com/. > > > > >Why not? :) -- Edd Morgan http://www.eddm.co.uk +44 (0) 7805 089097 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I have a situation where I need to update a custom counter periodically and don''t want to create a ton of overhead accomplishing this when I can do it with a simple update statement: update categories set categories.product_count = (select count(*) from categories_products where category_id = categories.id); The goal is to have a product_count on the categories table. However, the product has_and_belongs_to_many :categories So the traditional magic column doesn''t apply... unless I am missing the obvious here. On Oct 30, 8:43 am, "Edd Morgan" <thew...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On Oct 30, 2007 12:42 PM, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > > peri wrote: > > > Has anyone come across documentation on how to execute an > > > update_by_sql function on an ActiveRecord object? > > > This would be similar to X.find_by_sql. > > > There is no such method: > >http://api.rubyonrails.org/classes/ActiveRecord/Base.html > > > Why would you want to have update_by_sql? > > > Lutz > > -- > > Posted viahttp://www.ruby-forum.com/. > > Why not? :) > > -- > Edd Morganhttp://www.eddm.co.uk > +44 (0) 7805 089097--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Oct 30, 5:42 am, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> peri wrote: > > Has anyone come across documentation on how to execute an > > update_by_sql function on an ActiveRecord object? > > This would be similar to X.find_by_sql. > > There is no such method:http://api.rubyonrails.org/classes/ActiveRecord/Base.html > > Why would you want to have update_by_sql? > > Lutz > -- > Posted viahttp://www.ruby-forum.com/.AR::Base#connection.execute (i don''t remember if it''s a class or instance method, but search for it http://nick.recoil.org/2006/8/12/searching-for-a-rails-delete_by_sql-method --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gene - That did the trick! Thanks a lot! Here is an overview of what I ended up with, in case anyone else stumbles here... class Category < ActiveRecord::Base has_and_belongs_to_many :products, :order=>''products.name'' has_many :categories_products validates_uniqueness_of :name def self.update_product_counts sql = "update categories set categories.product_count = (select count(*) from categories_products where category_id = categories.id);" connection.update(sql) end end On Oct 30, 8:53 am, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 30, 5:42 am, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > peri wrote: > > > Has anyone come across documentation on how to execute an > > > update_by_sql function on an ActiveRecord object? > > > This would be similar to X.find_by_sql. > > > There is no such method:http://api.rubyonrails.org/classes/ActiveRecord/Base.html > > > Why would you want to have update_by_sql? > > > Lutz > > -- > > Posted viahttp://www.ruby-forum.com/. > > AR::Base#connection.execute (i don''t remember if it''s a class or > instance method, but search for it > > http://nick.recoil.org/2006/8/12/searching-for-a-rails-delete_by_sql-...--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
cu9ypd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-30 13:08 UTC
Re: update_by_sql ???
peri wrote:> I have a situation where I need to update a custom counter > periodically and don''t want to create a ton of overhead accomplishing > this when I can do it with a simple update statement: > > update categories set categories.product_count = (select count(*) from > categories_products where category_id = categories.id); > > The goal is to have a product_count on the categories table. However, > the product has_and_belongs_to_many :categories >May be you move to has_many :througth and magic columns will be back :)> So the traditional magic column doesn''t apply... unless I am missing > the obvious here. > > > > On Oct 30, 8:43 am, "Edd Morgan" <thew...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On Oct 30, 2007 12:42 PM, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> >> >> >> >> >>> peri wrote: >>> >>>> Has anyone come across documentation on how to execute an >>>> update_by_sql function on an ActiveRecord object? >>>> This would be similar to X.find_by_sql. >>>> >>> There is no such method: >>> http://api.rubyonrails.org/classes/ActiveRecord/Base.html >>> >>> Why would you want to have update_by_sql? >>> >>> Lutz >>> -- >>> Posted viahttp://www.ruby-forum.com/. >>> >> Why not? :) >> >> -- >> Edd Morganhttp://www.eddm.co.uk >> +44 (0) 7805 089097 >> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/30/07, gene tani <gene.tani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 30, 5:42 am, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > peri wrote: > > > Has anyone come across documentation on how to execute an > > > update_by_sql function on an ActiveRecord object? > > > This would be similar to X.find_by_sql.> AR::Base#connection.execute (i don''t remember if it''s a class or > instance method, but search for itActually it''s ActiveRecord::Base.connection.update(sql) It''s probably better to get the connection from the AR class whose instance you are updating. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
On Oct 30, 5:27 pm, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10/30/07, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Oct 30, 5:42 am, Lutz Horn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > peri wrote: > > > > Has anyone come across documentation on how to execute an > > > > update_by_sql function on an ActiveRecord object? > > > > This would be similar to X.find_by_sql. > > AR::Base#connection.execute (i don''t remember if it''s a class or > > instance method, but search for it > > Actually it''s > > ActiveRecord::Base.connection.update(sql) > > It''s probably better to get the connection from the AR class whose > instance you are updating. > -- > Rick DeNatale > > My blog on Rubyhttp://talklikeaduck.denhaven2.com/correct. Never a good sign when one of the top google hits is a trac saying there''s no docs for this method: http://railsdocs.lighthouseapp.com/projects/2639/tickets/4-no-api-documentation-for-activerecord-connection-execute-method --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---