我有一个小问题,通过Activerecord查询接口写 实际上是:
Gp.select("date('gps'.'created_at') as date,('users'.'name') as name, SUM('gps'.'sum_issue') as sum_issue").joins('LEFT JOIN users ON users.id = gps.user_id').where("users.ab_id = :abs_id AND users.id != 20", {:abs_id => current_user.ab_id}).group("users.name")
查询结果必须是用户名,总和和日期。如果我直接从SQLlite执行此查询,它的工作,但是 Active Record Query Interface给我
[#<Gp sum_issue: 289000>, #<Gp sum_issue: 364130>, #<Gp sum_issue: 620000>]
我如何获得姓名,日期,sum_issue并在我的助手中显示。 像这样:
{
created_at: datet,
sum_issue: sum_issue,
name: name
}
答案 0 :(得分:1)
尝试
Gp.select("date('gps'.'created_at') as date,('users'.'name') as name, SUM('gps'.'sum_issue') as sum_issue").joins('LEFT JOIN users ON users.id = gps.user_id').where("users.ab_id = :abs_id AND users.id != 20", {:abs_id => current_user.ab_id}).group("users.name").first.name