On Thu, Jan 22, 2009 at 12:38 PM, Max Williams < [email protected]> wrote:
> > I have an association user.lessons. Sometimes i just want the lessons, > other times i want the lessons to eager-load one of their own > associations, 'assets'. Theres a couple of ways i can see to deal with > this and i'm not super happy with any of them. > > approach 1) Make two associations: > has_many :lessons > has_many :lessons_with_assets, :class_name => "Lesson", :include => > [:assets] > > #then, if i want the assets, call > @user.lessons_with_assets > > This is actually my favorite but it still feels a bit clumsy > > approach 2) Don't use the association if i want to eager load > #just do a regular find > Lesson.find_all_by_user_id(@user.id, :include => [:assets]) > > Is there another way? This example is simple but i have other > associations that go through join tables and are polymorphic (eg using > the has_favorite plugin) that are a bit more arsey to use. > > thanks > max > -- > Posted via http://www.ruby-forum.com/. > > > > Max There's another way, named_scopes. in the Lesson model, add named_scope :with_assets, :include => :assets then you can call lessons or lessons.with_assets -- Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

