将SQL查询转换为rails Active Record查询界面

时间:2011-12-15 08:35:48

标签: sql ruby-on-rails ruby activerecord

我有以下sql查询,我想将它映射到rails count查询:

SELECT count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date 
FROM video_logs 
where user_id = 19 and question_id = 96 and dashboard = 'player_question' 
GROUP BY converted_date;

我该怎么做?

1 个答案:

答案 0 :(得分:4)

在rails 3中:

VideoLog.select("count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date").\
    where(:user_id => 19, :question_id => 96, :dashboard => 'player_question').\
    group('converted_date')

在rails 2中:

VideoLog.all(:select => "count(*), DATE(CONVERT_TZ(created_at, '+00:00', '-05:00')) as converted_date",
    :conditions => {:user_id => 19, :question_id => 96, :dashboard => 'player_question'},
    :group => 'converted_date')