0

当我跑步时

             sql = "select count( school_id ) as vote_total,
                  row_number() over( order by  count( school_id ) 
                  DESC ) as rank, school_id
                  from votes
                  where school_id = 111111
                  group by school_id"

在 phpPgAdmin 中,我得到了我正在寻找的东西

      vote_total rank school_id
             68    1  111111

但是当我对 Vote.find_by_sql 做同样的事情时,我得到了

     [#<Vote id: nil, school_id: 111111>]

我猜 find_by_sql 不是我应该使用的。关于正确方法的任何想法?

4

1 回答 1

2

你可以得到下面的 vote_total 和 school_id

sql = "select count( school_id ) as vote_total,
                  row_number() over( order by  count( school_id ) 
                  DESC ) as rank, school_id
                  from votes
                  where school_id = 111111
                  group by school_id"

votes = Vote.find_by_sql(sql)
votes.first["vote_total"] #or votes.first.vote_total
voted.first["school_id"] #or votes.first.school_id

由于 vote_total 和 school_id 不是 Vote 的属性,因此没有为这些属性定义 getter 方法

于 2013-10-05T20:12:57.850 回答