Ruby-on-rails Rails 控制台 - 查找在 = 特定日期创建的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21410149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:18:28  来源:igfitidea点击:

Rails Console - Find where created at = certain day

ruby-on-railsrubydatabasepostgresqlrails-console

提问by Seth

With Ruby on Rails console, is it possible to query the database for all records created on a certain day?

使用 Ruby on Rails 控制台,是否可以查询数据库中某一天创建的所有记录?

something like

就像是

date = "january 5 2013"
users = User.find(:all, :conditions => {:created_at => date})

回答by Agis

You can do it like this:

你可以这样做:

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

回答by Praveen Kumar

Try this,

尝试这个,

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

回答by M. Habib

You can also do it like

你也可以这样做

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

回答by poramo

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

如果您看到以下链接不要使用 BETWEEN(尤其是带有时间戳), 您会注意到您可能会遇到问题。

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')"

回答by Dheer

Yes, It is possible like:

是的,它可能像:

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

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

但是created_at是日期时间类型2014-01-28 08:35:00.9608

and I think All user have different created_at

我认为所有用户都有不同的 created_at

So you may used like this

所以你可以这样使用

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