courses_users(course_id, user_id, user_type)
Firstly, this looks more like a :has_many_through rather than a
Secondly, this is not as "Simple?" a query as you might expect.
Although this is a fairly common need.
In SQL you would need to use a JOIN to accomplish this. I'm sure you
could probably find a design pattern or two for this type of query if
you search the web for it. I'm sorry I don't have any links off hand.
The most efficient way is to search against a resource that joins user
and courses. Only the join table has knowledge of both sides of your
Let's take a look at the SQL approach:
SELECT DISTINCT courses.id, courses.title FROM courses JOIN
courses_users ON courses.id=courses_users.course_id WHERE
courses_users.user_type = "instructor_confirmed";
This would give you the list of DISTINCT courses. You need the
DISTINCT to eliminate the duplicate rows that would be returned by the
In Rails you could just fetch all the courses_users resources, which
will be more than you need due to the duplicates, and then iterate
over the results adding them to an array while ensuring you don't add
in the duplicates. Or possibly use Array#uniq to get an array with
I'm sure there's probably a better way than this, but it should work
and be relatively efficient.