Ruby-on-rails Rails ActiveRecord 日期之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2381718/
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 22:21:18  来源:igfitidea点击:

Rails ActiveRecord date between

ruby-on-railsrails-activerecordruby-on-rails-2

提问by rtacconi

I need to query comments made in one day. The field is part of the standard timestamps, is created_at. The selected date is coming from a date_select.

我需要查询一天的评论。该字段是标准时间戳的一部分,是created_at. 所选日期来自date_select.

How can I use ActiveRecordto do that?

我怎样才能ActiveRecord做到这一点?

I need something like:

我需要类似的东西:

"SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'"

回答by ndbroadbent

Just a note that the currently accepted answer is deprecated in Rails 3. You should do this instead:

请注意,当前接受的答案在 Rails 3 中已弃用。您应该这样做:

Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day)

Or, if you want to or have to use pure string conditions, you can do:

或者,如果您想或必须使用纯字符串条件,您可以执行以下操作:

Comment.where('created_at BETWEEN ? AND ?', @selected_date.beginning_of_day, @selected_date.end_of_day)

回答by Marshall Shen

I would personally created a scope to make it more readable and re-usable:

我会亲自创建一个范围,使其更具可读性和可重用性:

In you Comment.rb, you can define a scope:

在您的 Comment.rb 中,您可以定义一个范围:

scope :created_between, lambda {|start_date, end_date| where("created_at >= ? AND created_at <= ?", start_date, end_date )}

Then to query created between:

然后在以下之间创建查询:

@comment.created_between(1.year.ago, Time.now)

Hope it helps.

希望能帮助到你。

回答by Bismark

Rails 5.1 introduced a new date helper method all_day, see: https://github.com/rails/rails/pull/24930

Rails 5.1 引入了新的日期辅助方法all_day,参见:https: //github.com/rails/rails/pull/24930

>> Date.today.all_day
=> Wed, 26 Jul 2017 00:00:00 UTC +00:00..Wed, 26 Jul 2017 23:59:59 UTC +00:00

If you are using Rails 5.1, the query would look like:

如果您使用的是 Rails 5.1,查询将如下所示:

Comment.where(created_at: @selected_date.all_day)

回答by Irukandji

This code should work for you:

此代码应该适合您:

Comment.find(:all, :conditions => {:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day})

For more info have a look at Time calculations

有关更多信息,请查看时间计算

Note:This code is deprecated. Use the code from the answer if you are using Rails 3.1/3.2

注意:此代码已弃用。如果您使用的是 Rails 3.1/3.2,请使用答案中的代码

回答by boulder_ruby

I ran this code to see if the checked answer worked, and had to try swapping around the dates to get it right. This worked--

我运行此代码以查看检查的答案是否有效,并且不得不尝试交换日期以使其正确。这奏效了——

Day.where(:reference_date => 3.months.ago..Time.now).count
#=> 721

If you're thinking the output should have been 36, consider this, Sir, how many days is 3 days to 3 people?

如果您认为输出应该是 36,请考虑一下,先生,3 天到 3 个人是多少天?

回答by kaushal sharma

Comment.find(:all, :conditions =>["date(created_at) BETWEEN ? AND ? ", '2011-11-01','2011-11-15'])

回答by nroose

I have been using the 3 dots, instead of 2. Three dots gives you a range that is open at the beginning and closed at the end, so if you do 2 queries for subsequent ranges, you can't get the same row back in both.

我一直在使用 3 个点,而不是 2 个。三个点为您提供一个开始时打开并在最后关闭的范围,因此如果您对后续范围进行 2 次查询,则无法返回同一行两个都。

2.2.2 :003 > Comment.where(updated_at: 2.days.ago.beginning_of_day..1.day.ago.beginning_of_day)
Comment Load (0.3ms)  SELECT "comments".* FROM "comments" WHERE ("comments"."updated_at" BETWEEN '2015-07-12 00:00:00.000000' AND '2015-07-13 00:00:00.000000')
=> #<ActiveRecord::Relation []> 
2.2.2 :004 > Comment.where(updated_at: 2.days.ago.beginning_of_day...1.day.ago.beginning_of_day)
Comment Load (0.3ms)  SELECT "comments".* FROM "comments" WHERE ("comments"."updated_at" >= '2015-07-12 00:00:00.000000' AND "comments"."updated_at" < '2015-07-13 00:00:00.000000')
=> #<ActiveRecord::Relation []> 

And, yes, always nice to use a scope!

而且,是的,使用示波器总是很好!

回答by Augustin Riedinger

There should be a default active record behavior on this I reckon. Querying dates is hard, especially when timezones are involved.

我认为应该有一个默认的活动记录行为。查询日期很困难,尤其是在涉及时区时。

Anyway, I use:

无论如何,我使用:

  scope :between, ->(start_date=nil, end_date=nil) {
    if start_date && end_date
      where("#{self.table_name}.created_at BETWEEN :start AND :end", start: start_date.beginning_of_day, end: end_date.end_of_day)
    elsif start_date
      where("#{self.table_name}.created_at >= ?", start_date.beginning_of_day)
    elsif end_date
      where("#{self.table_name}.created_at <= ?", end_date.end_of_day)
    else
      all
    end
  }

回答by klew

If you only want to get one day it would be easier this way:

如果你只想得到一天,这样会更容易:

Comment.all(:conditions => ["date(created_at) = ?", some_date])

回答by ben

there are several ways. You can use this method:

有几种方法。您可以使用此方法:

start = @selected_date.beginning_of_day
end = @selected_date.end_of_day
@comments = Comment.where("DATE(created_at) BETWEEN ? AND ?", start, end)

Or this:

或这个:

@comments = Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day)