简体   繁体   中英

Convert SQL query to rails Active Record query interface

I have the following sql query and I want to map it to a rails count query:

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;

How do I do this?

In 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')

In 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM