I have four tables: "users", "user_groups", "groups", "categories".
"users" and "groups" are many_to_many relations through "user_groups".
"groups" and "categories" are many_to_one relations.
I created the following SQL query, but I'm not sure how to implement it in Ruby on Rails:
SELECT u.*
FROM users u
WHERE EXISTS (SELECT 1
FROM user_groups ug,
groups g,
categories c
WHERE u.id = ug.user_id
AND ug.group_id = g.id
AND g.category_id = c.id
AND c.id in ('1, 2, 3'))
What is the best way to implement it without using raw SQL in Ruby on Rails?