Hey I have a DB Model with the following tables users, courses, users_courses and coursecategories users <1:n> users_courses <n:1> courses <n:1> I have the models set up and they sheem to work. I can do something like this user_courses = User.find_by_id(1).courses (resolves the n:m relation and returns the right course objects) What i need is something like this: user_categories = User.find_by_id(1).courses.coursecategorie (doesn''t work) I want to query all the categories a user has courses in. How do i do that? thx -- 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 -~----------~----~----~----~------~----~------~--~---
Sheems to work with: User.find_by_id(1).courses.collect { |c| c.coursecategorie }.flatten ck Christian Kerth wrote:> Hey I have a DB Model with the following tables > > users, courses, users_courses and coursecategories > > users <1:n> users_courses <n:1> courses <n:1> > > I have the models set up and they sheem to work. > > I can do something like this > > user_courses = User.find_by_id(1).courses (resolves the n:m relation and > returns the right course objects) > > What i need is something like this: > > user_categories = User.find_by_id(1).courses.coursecategorie (doesn''t > work) > > I want to query all the categories a user has courses in. > > How do i do that? > > thx-- 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 11 Jan 2008, at 11:34, Christian Kerth wrote:> > Hey I have a DB Model with the following tables > > users, courses, users_courses and coursecategories > > users <1:n> users_courses <n:1> courses <n:1> > > I have the models set up and they sheem to work. > > I can do something like this > > user_courses = User.find_by_id(1).courses (resolves the n:m relation > and > returns the right course objects) > > What i need is something like this: > > user_categories = User.find_by_id(1).courses.coursecategorie (doesn''t > work) > > I want to query all the categories a user has courses in. > > How do i do that?Unfortunately you can''t nest has_many :throughs at a more basic level, CourseCategory.find :all, :joins => :user_courses, :conditions => [''user_courses.user_id = ?'', 1] Should get them(possibly with duplicates though) 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 -~----------~----~----~----~------~----~------~--~---