我想写下面的查询rails方式

时间:2014-02-03 12:33:45

标签: mysql sql ruby ruby-on-rails-3

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 )

但似乎没有任何帮助? 提前谢谢

1 个答案:

答案 0 :(得分:1)

User.select("users.firstname").includes(:members => [{:member_roles => :role}]).where("roles.name" => "Project Coordinator")