jcoglan-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-May-14 09:34 UTC
Default :include value for ActiveRecord::Base.find
Hi all, I''m working on a application with lots of complicated data relationships and thousands of rows of data. To speed up queries, I''m using Project.find(:all, :conditions => some_hash, :include => some_other_hash). I''m using the same hash for :include quite a lot - is there a way to specify in the Project model that it should use a certain :include hash in find() by default so I can DRY up my code? Thanks, James --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi You can always define a class method for Project that wraps the find, something like (warn absolutely untested) class Project def self.find_std_include(*args) options = {:include => your_standard_include_hash} if args.last.is_a? Hash options.update(args.pop) end self.find(*args.push(options)) end end This way you can call Project.find_std_include() every time you need to use the standard include clause. Paolo On 14/05/07, jcoglan-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <jcoglan-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Hi all, > > I''m working on a application with lots of complicated data > relationships and thousands of rows of data. To speed up queries, I''m > using Project.find(:all, :conditions => some_hash, :include => > some_other_hash). I''m using the same hash for :include quite a lot - > is there a way to specify in the Project model that it should use a > certain :include hash in find() by default so I can DRY up my code? > > Thanks, > James--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
jcoglan-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-May-14 11:10 UTC
Re: Default :include value for ActiveRecord::Base.find
Thanks Paolo. I figured a class method was probably the way to go, but was wondering if there was any way of modifying the default behaviour of find() on a per-class basis. Thanks for the help. On May 14, 11:33 am, "Paolo Negri" <hungryl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > You can always define a class method for Project that wraps the find, > something like (warn absolutely untested) > > class Project > def self.find_std_include(*args) > options = {:include => your_standard_include_hash} > if args.last.is_a? Hash > options.update(args.pop) > end > self.find(*args.push(options)) > end > end > > This way you can call Project.find_std_include() every time you need > to use the standard include clause. > > Paolo > > On 14/05/07, jcog...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <jcog...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > Hi all, > > > I''m working on a application with lots of complicated data > > relationships and thousands of rows of data. To speed up queries, I''m > > using Project.find(:all, :conditions => some_hash, :include => > > some_other_hash). I''m using the same hash for :include quite a lot - > > is there a way to specify in the Project model that it should use a > > certain :include hash in find() by default so I can DRY up my code? > > > Thanks, > > James--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Florian Gilcher
2007-May-14 11:31 UTC
Re: Default :include value for ActiveRecord::Base.find
Sure, there is a way. class Project class << self alias find_without_standard_includes find def find_with_standard_includes(*args) options = {:include => your_standard_include_hash} if args.last.is_a? Hash options.update(args.pop) end self.find_without_standard_includes(*args.push(options)) end alias find find_with_standard_includes end end If you do not understand the class << self syntax: Every Method inside class << self .... end is considered a method of the self object, that is the class itself in this context. This is needed, because alias does only work with method names (alias self.find_without_standard_includes self.find does not work). Have fun Florian jcoglan-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Thanks Paolo. I figured a class method was probably the way to go, but > was wondering if there was any way of modifying the default behaviour > of find() on a per-class basis. Thanks for the help.-- 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 -~----------~----~----~----~------~----~------~--~---