2014-01-31 181 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