Is it possible to make a model always eager load an association, without explicitly specifying it every time? For example: class Article < AR::Base has_one :author end I''m *always* going to need the Author for the Article, so is there a way to set :include => :author by default for find()? I thought about overwriting find() on Article and then just call super() with the :include, but I''m not exactly sure how that would go. Any ideas? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-17 16:14 UTC
Re: Always eager load
This seems to work: def self.find(*args) options = extract_options_from_args!(args) options[:include] ||= :author super(args.first, options) end Is there an easier way or are there any problems with this approach? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If there''s already an options[:include] you''re going to use it then, right? [That''s what the code says.] If you''re wanting to append it to any other manual :include, you''ll need to convert options[:include] to an array and all. If there''s no other possible models to include then you could options[:include] = :author instead. But I''d say you''ve got it solved already. RSL On 2/17/07, toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > This seems to work: > > def self.find(*args) > options = extract_options_from_args!(args) > options[:include] ||= :author > super(args.first, options) > end > > Is there an easier way or are there any problems with this approach? > > Thanks > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-18 04:49 UTC
Re: Always eager load
Actually, it''s the opposite, if there is an :include defined then it does nothing, this way I can specify :include => nil if I didn''t want to eager load (which won''t happen, but just in case). That seems to do the job then, thank you. Feb 17, 2:27 pm, "Russell Norris" <sco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If there''s already an options[:include] you''re going to use it then, right? > [That''s what the code says.] If you''re wanting to append it to any other > manual :include, you''ll need to convert options[:include] to an array and > all. If there''s no other possible models to include then you could > options[:include] = :author instead. But I''d say you''ve got it solved > already. > > RSL > > On 2/17/07, tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > This seems to work: > > > def self.find(*args) > > options = extract_options_from_args!(args) > > options[:include] ||= :author > > super(args.first, options) > > end > > > Is there an easier way or are there any problems with this approach? > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---