开发者

ActiveRecord Query with many to many or other Params

I got 2 Models

  • Projects (with a boolean column "hidden")
  • Models

the both have a many to many relation开发者_开发问答ship, 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?


Project.joins("projects_users").where("hidden = ? OR projects_users.user_id = ?", false, current_user.id).group("projects.id")


All you have to do is create a scope for both of them. Then you can test them with your rails console window

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜