我很好奇是否有一种方法可以在不调用 Postgres 的情况下使用 where
方法从日期中查找日期时间。
我一直通过注入(inject) SQL 来完成此操作,但我想知道是否有一种简单的方法可以完成我一直忽略的同一件事(因为我一直在这样做)。
SomeModel.where("start_at::timestamp::date = ?", '2012-01-01') #where start_at is a DateTime column
谢谢,
最佳答案
嗯……你可以使用一个范围来跨越一整天:
SomeModel.where(start_at: date.beginning_of_day..date.end_of_day) #date = "2012-01-01".to_date
不确定是否有比这更简洁的方法。
关于ruby-on-rails - rails 4 where 日期时间字段上的日期方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543563/