2014-07-16 30 views
0

当我在DB下表:字符串转换为日期时间使ActiveRecord的数据库查询

class CreateGoogleRecords < ActiveRecord::Migration 
def change 
    create_table :google_records do |t| 
    t.string :user_id 
    t.string :date 
    t.text :stats 
    t.string :account_name 
    t.integer :total_conversions 
    t.decimal :total_cost 

    t.timestamps 
    end 
end 
end 

我希望创建一个视图中一个表,按组记录每月一起(我不能使用“的日期,因为有时他们是从API批量刮)

有很多涉及的遗留代码,而不是将列转换为datetime我希望我可以将日期字符串转换为日期时间对象时执行查询。

我试过了写g范围如下:

scope :stats_for_reports, ->(start_date, end_date, user_ids) { select('user_id, sum(total_cost) as total_cost, sum(total_conversions) as total_conversions') 
                   .where('date >= ? and date <= ?', start_date, end_date) 
                   .where(user_id: user_ids) 
                   .group(DateTime.parse(:date).month.to_s)} 

但我收到TypeError: can't convert Symbol into String错误。

在控制台中我一直喜欢的东西:

GoogleRecord.where(date: date_start..date_end).group{ |m| DateTime.parse(m.date).month } 

GoogleRecord.where(date: date_start..date_end).group(:date).to_date 

我在正确的轨道上任何这些吗?

回答

相关问题