轨道3日期时间比较瓦特/日期在一个ActiveRecord查询 [英] Rails 3 DateTime Comparison w/ Date In An ActiveRecord Query

查看:138
本文介绍了轨道3日期时间比较瓦特/日期在一个ActiveRecord查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找一种模式等于一个特定日期的日期,而忽略时间戳。在Rails,我可以简单地执行本作 DateTime.to_date == somedate ,但我不认为它很容易在SQL中制定的地方我就无法应用TO_​​DATE方法来整列像created_at:

I'm trying to search a model for any dates equal to a specific date while omitting the timestamp. In Rails I could simply execute this as DateTime.to_date == somedate, however I don't think it's quite as easy to formulate in SQL where I wouldn't be able to apply the to_date method to an entire column like created_at:

Foo.where("created_at == some_day_without_time_stamp").count

起初我还以为是因为我使用的是PostgreSQL数据库,我可以简单地使用psql的语法,但我还是更愿意把它留给ActiveRecord的决定什么SQL是最适用的,让我的code无关的数据库厂商。这是可能的,没有任何额外的插件或宝石?

Initially I thought that because I was using a postgresql database I could simply use psql syntax, but I much much rather leave it to ActiveRecord to decide what sql is most applicable and keep my code agnostic of database vendors. Is this possible without any additional plugins or gems?

推荐答案

尝试 created_at> = some_day_with_00:00:00时间戳和create_at< some_day_plus_one_with_00:00:00时间戳

这篇关于轨道3日期时间比较瓦特/日期在一个ActiveRecord查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆