Rails 3 has_many :through + join table conditions / scoping

前端 未结 3 365
有刺的猬
有刺的猬 2021-02-01 10:36

I\'m working on an app that has the models User and Project, and User can be assigned to multiple Projects, via Project

3条回答
  •  庸人自扰
    2021-02-01 10:42

    Did you try using scopes yet? It doesn't let you do <<. But it simplifies querying.

    Try:

    Project
      scope :developers, lambda {
        includes(:project_users).where("project_users.role = ?", "developer")
      }
    

    You will be able to get all developers using: @project.developers

提交回复
热议问题