1
@project_list =  Project.find_by_sql("select p.id, p.name, (select sum(i.estimated_hours) from issues i where  i.project_id = p.id)  as estimated_hours,(select sum(t.hours)  from time_entries t where p.id = t.project_id )  as Spent_Hours,(select u.firstname  from users u  where u.id IN(select user_id from members m where m.project_id = p.id and m.id IN   ( select member_id from member_roles   where role_id IN (select id from roles r where r.name  = 'Project Coordinator') )) limit 1 ) as Coordinator,(select u.firstname  from users u  where u.id IN(select user_id from members m where m.project_id = p.id and m.id IN   ( select member_id from member_roles   where role_id IN (select id from roles r where r.name  = 'Project Manager') )) limit 1) as  Manager   from projects p where p.status IN ('16','14','15','17','18','19','20')   group by p.id")

使用上面的查询我可以在我的视图页面中获取记录现在我想进一步排序或搜索,即按协调员、开始日期、结束日期等但我不能在结果数组上应用搜索方法,因为 find_by_sql 返回一个数组。虽然上面的查询似乎很难使用 rails 方式编写所以

  1. 有没有其他方法可以制作只读模型或类似的东西或
  2. 任何人都可以帮助我使用 Rails 方式编写此查询。

任何帮助或建议都会有很大帮助

4

0 回答 0