ActiveRecord Join over multiple tables

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

Sheems to work with:

User.find_by_id(1).courses.collect { |c| c.coursecategorie }.flatten

ck

Christian K. 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

On 11 Jan 2008, at 11:34, Christian K. wrote:

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