Mysql 将SQL查询转换为ActiveRecord查询

Mysql 将SQL查询转换为ActiveRecord查询,mysql,ruby-on-rails,active-record-query,Mysql,Ruby On Rails,Active Record Query,我有一个在SQLite数据库上运行的SQL查询。检索周六和周日上午7点到下午6点之间的日期范围内的记录效果良好 当我运行查询时,它返回一个哈希数组。我想将其转换为ActiveRecord查询,以便它返回一个对象数组,我可以更轻松地对其进行操作 ActiveRecord::Base.connection.execute("select * from reports where datetime between '2015-03-25' and '2015-04-12' and strftime('

我有一个在SQLite数据库上运行的SQL查询。检索周六和周日上午7点到下午6点之间的日期范围内的记录效果良好

当我运行查询时,它返回一个哈希数组。我想将其转换为ActiveRecord查询,以便它返回一个对象数组,我可以更轻松地对其进行操作

ActiveRecord::Base.connection.execute("select * from reports where datetime between '2015-03-25' and '2015-04-12' and strftime('%w', datetime) IN ('0','6') and strftime('%H', datetime) >= '07' and strftime('%H:%M', datetime)
ActiveRecord::Base.connection.execute(“从报告中选择*,其中datetime介于'2015-03-25'和'2015-04-12'之间,strftime('%w','datetime')位于('0','6'),strftime('%H','datetime)>='07'和strftime('%H:%M',datetime))
我已经看过了,不知道该怎么做,如果有任何帮助,我将不胜感激

更新: 非常接近这一点:

Report.where("datetime >= ? AND datetime <= ?", '2015-03-25 21:15:00', '2015-04-27 07:56:00').where("cast(strftime('%H', datetime) as int) >= ? AND cast(strftime('%H', datetime) as int) <= ?", 07,17).where("cast(strftime('%w', datetime) as int) = ?", 0)

Report.where(“datetime>=”和datetime=?并强制转换(strftime(“%H”,datetime)为int)对于这个复杂的SQL查询字符串,我认为您应该使用
ActiveRecord
find\u by\u SQL
。这里的文档:


对于这个复杂的SQL查询字符串,我认为应该使用
ActiveRecord
find\u by\u SQL

Report.find_by_sql("select * from reports where datetime between '2015-03-25' and '2015-04-12' and strftime('%w', datetime) IN ('0','6') and strftime('%H', datetime) >= '07' and strftime('%H:%M', datetime)