简体   繁体   English

rails 4 where datetime字段上的日期方法

[英]rails 4 where method for date on a datetime field

I'm curious if there's a way to use the where method without invoking Postgres to find a datetime from a date. 我很好奇是否有一种方法可以使用where方法而不调用Postgres从日期中查找日期时间。

I've been accomplishing this by just injecting SQL but was wondering if there were a railsy way of accomplishing the same thing that I've been overlooking (as I've been doing this a lot). 我一直在通过注入SQL来完成此任务,但想知道是否有一种怪异的方式来完成我一直忽略的同一件事(因为我已经做了很多事情)。

SomeModel.where("start_at::timestamp::date = ?", '2012-01-01') #where start_at is a DateTime column

Thanks, 谢谢,

Hmmm... well you could use a range to span across the entire day: 嗯...好吧,您可以使用范围跨一整天:

SomeModel.where(start_at: date.beginning_of_day..date.end_of_day) #date = "2012-01-01".to_date  

Not sure there's an cleaner way than that. 不确定是否有比这更清洁的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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