【发布时间】:2018-08-06 13:57:15
【问题描述】:
我希望在 py projects_controller 中定义项目的索引。
def index
@projects = Project.where(user_id: current_user.id) #but also need to pull in Projects that have collaborators
end
项目有_一个“用户”作为组织者,但也有_许多“合作者”属于_项目。
我希望 @projects 返回用户创建或合作的任何项目的集合。
项目模型:
class Project < ApplicationRecord
belongs_to :user
has_one :chat_room
has_many :collaborators
end
合作者模式
class Collaborator < ApplicationRecord
belongs_to :project
end
我认为解决方案是添加一个“或”语句
@projects = Project.where(user_id: current_user.id) || Project.joins(:collaborators).where(collaborator: {email: current_user.email})
但这不起作用。
【问题讨论】:
标签: ruby-on-rails