0

我有 UserStat 表,其属性和值定义为字符串...当我从 user_stats where property = 'BEST_SCORE'" 中选择 (CAST(value as integer) ) 时,一切都很好:

UserStat.find_by_sql(" select (CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'") UserStat Load (8.6ms) select (CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'

=> [#< UserStat id: nil, value: 869>]

但是当我这样做时:

UserStat.find_by_sql(" select AVG(CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'")

UserStat Load (9.7ms) select AVG(CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'

=> [#< UserStat id: nil>]

我的值字段在哪里???我想我必须得到 869 作为价值..

4

1 回答 1

0

我用 a = ActiveRecord::Base.connection.select_all("select AVG(CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'") a[0]['avg'].to_i 解决了问题

但是仍然认为 UserStat.find_by_sql(" select AVG(CAST(value as integer) ) from user_stats where property = 'BEST_SCORE'") 必须引发错误或返回值..

于 2013-08-24T06:29:03.380 回答