数组的未定义方法

时间:2013-10-23 14:17:41

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

我有这个查询,但是我收到了这个错误:未定义的方法`where'for #Array:0x0000000443cd18

@kpig = KpiGroups.find_by_sql('select * from (
      select lk.login,sdf.* from (select kg.id, kg.name as "GROUP_NAME", kt.name, kt.id as "KPI"
      from spartacus.kpi_groups kg, spartacus.kpi_types kt left outer join (select kpiid as kpi, max(max_value) as max_val ,min(min_value) as min_val
      from (select kpiid,max_value,min_value from spartacus.stations group by  max_value,min_value,kpiid) group by kpiid) sel on(sel.kpi=kt.id) where  kt.parentcatalogid=kg.id) sdf
      left outer join spartacus.LKBs lk on lk.kpi_id=sdf."KPI" )').where("login = '#{current_user.login}'")

2 个答案:

答案 0 :(得分:1)

find_by_sql方法返回Array而非ActiveRecord::Relation实例。

答案 1 :(得分:1)

你可以这样做:

    @kpig = KpiGroups.find_by_sql("select * from (
  select lk.login,sdf.* from (select kg.id, kg.name as GROUP_NAME, kt.name, kt.id as KPI
  from spartacus.kpi_groups kg, spartacus.kpi_types kt left outer join (select kpiid as kpi, max(max_value) as max_val ,min(min_value) as min_val
  from (select kpiid,max_value,min_value from spartacus.stations group by  max_value,min_value,kpiid) group by kpiid) sel on(sel.kpi=kt.id) where  kt.parentcatalogid=kg.id) sdf
  left outer join spartacus.LKBs lk on lk.kpi_id=sdf.KPI ) where login='#{current_user.login}'")