2014-01-31 182 views
-1

此查询没有按;吨似乎工作SQL查询轨方式不起作用

Project.find_by_sql("select p.name as n,u.firstname,sum(i.estimated_hours) as es,(select sum(hours) as hours from time_entries where time_entries.user_id = u.id and time_entries.project_id = p.id) as spenthours from users u inner join members m on (m.user_id= :id)inner join projects p on p.id = m.project_id inner join issues i on i.assigned_to_id = u.id and i.project_id = p.id group by p.id,u.id",{:id => params[:id]}) 

给我的错误,如

ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':id inner join projects p on p.id = m.project_id inner join issues i on i.assign' at line 1: 

有任何的方式来传递PARAMS在轨的find_by_sql查询。我试过但似乎不工作。如果有任何其他合适的方式,然后PLZ显示我的方式

回答

3

您需要将find_by_sql的参数包装在一个数组中:

Project.find_by_sql(["you_great_query_here", {:id => params[:id]}]) 

检查documentation