select DISTINCT firstname
from users u
inner join members m
on m.user_id = u.id
inner join member_roles m1
on m1.member_id = m.id
inner join roles r
on r.id = m1.role_id
where r.name = 'Project Coordinator'
这给出了完美的结果
我试着写上面的查询rails方式 像
s = MemberRole.joins(:role).where("roles.name" => "Project Coordinator")
@u = User.select("users.firstname").includes(:members => s )
但似乎没有任何帮助? 提前谢谢
答案 0 :(得分:1)
User.select("users.firstname").includes(:members => [{:member_roles => :role}]).where("roles.name" => "Project Coordinator")