Ruby on rails 进行activerecord db查询时将字符串转换为日期时间
我的数据库中有下表:Ruby on rails 进行activerecord db查询时将字符串转换为日期时间,ruby-on-rails,activerecord,Ruby On Rails,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_
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
或
我对其中任何一项都正确吗?您是否考虑过在保存之前、保存之后、初始化之后使用ActiveRecord?您可以创建一个日期包装器(非常类似于下面的EncryptionWrapper),并将字符串转换为对代码其余部分透明的日期
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)}
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