Rails Console - Find where created at = certain day

落爺英雄遲暮 提交于 2019-12-02 20:21:10

You can do it like this:

date = Date.parse('january 5 2013')
users = User.where(created_at: date.midnight..date.end_of_day)
Dheer

Yes, It is possible like:

date = Date.parse("january 5 2013")
users = User.where(created_at: date)

but created_at is type of date-time like 2014-01-28 08:35:00.9608

and I think All user have different created_at

So you may used like this

User.where("created_at = ?", "2014-01-23 16:19:48.199086")

Try this,

User.where("Date(updated_at) = ?", "2018-02-9")

You can also do it like

User.where("created_at::date = ?", "january 5 2013".to_date)

If you see the follow link Don't use BETWEEN (especially with timestamps) You will note that you can have problems.

Instead you can use

users = User.where('created_at >= ? and created_at <=?', date.midnight, date.end_of_day)



"SELECT \"users\".* FROM \"users\" WHERE (created_at >= '2018-05-17 07:00:00' and created_at <='2018-05-18 06:59:59.999999')"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!