Rails 3 has_many :through + join table conditions / scoping

前端 未结 3 399
有刺的猬
有刺的猬 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

    It sounds like what you're looking for is a combination of RoR's single table inheritance and named scopes.

    Take a look at the following article for a nice example about polymorphic associations. This should help you with achieving the following:

    @project.developers
      # returns @project.users, but only where ProjectUser.role = 'Developer'
    
    @project.designers << @user
      # creates a ProjectUser for @project, @user with role 'Designer'
    

    Scopes will give you a clean way to implement @user.development_projects but there may be more trickery required to get the << operator.

提交回复
热议问题