简体   繁体   中英

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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