Reputation: 25244
I got 2 Models
the both have a many to many relationship, so i added a migration with a projects_users table and changed the models using
has_and_belongs_to_many :users
# and
has_and_belongs_to_many :projects
Now i want to do the following query:
Select all Projects where hidden is false OR "current_user" (a variable) belongs to project
Project.where("hidden = ?", false) and current_user.projects works. But how to combine them into one query?
Upvotes: 0
Views: 401
Reputation: 4089
All you have to do is create a scope for both of them. Then you can test them with your rails console window
Upvotes: 0
Reputation: 64177
Project.joins("projects_users").where("hidden = ? OR projects_users.user_id = ?", false, current_user.id).group("projects.id")
Upvotes: 1