代码之家  ›  专栏  ›  技术社区  ›  rtacconi

Rails ActiveRecord日期介于

  •  144
  • rtacconi  · 技术社区  · 15 年前

    我需要查询一天内的评论。该字段是标准时间戳的一部分,是 created_at date_select .

    ActiveRecord 这么做?

    我需要像这样的东西:

    "SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'"
    
    11 回复  |  直到 5 年前
        1
  •  432
  •   thutt    8 年前

    请注意,Rails 3中不推荐当前接受的答案。您应该改为:

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

    或者,如果您想或必须使用 pure string conditions ,您可以执行以下操作:

    Comment.where('created_at BETWEEN ? AND ?', @selected_date.beginning_of_day, @selected_date.end_of_day)
    
        2
  •  51
  •   Marshall Shen    11 年前

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

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

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

    然后要查询在之间创建的:

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

    希望有帮助。

        3
  •  34
  •   Bismark    7 年前

    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
    

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

    Comment.where(created_at: @selected_date.all_day)
    
        4
  •  25
  •   Pigueiras    12 年前

    此代码应适用于您:

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

    更多信息请看 Time calculations

    注: 这个代码是 deprecated

        5
  •  11
  •   boulder_ruby    12 年前

    我运行了这段代码以查看检查过的答案是否有效,并且必须尝试交换日期以获得正确答案。这起作用了--

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

        6
  •  7
  •   Aziz Shaikh    13 年前
    Comment.find(:all, :conditions =>["date(created_at) BETWEEN ? AND ? ", '2011-11-01','2011-11-15'])
    
        7
  •  5
  •   Sam Starling    9 年前

    我用的是3个点,而不是2个点。3个点给出的区域在开始时是打开的,在结束时是关闭的,所以如果你对后续的区域进行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 []> 
    

        8
  •  4
  •   klew    15 年前

    Comment.all(:conditions => ["date(created_at) = ?", some_date])
    
        9
  •  4
  •   ben    10 年前

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

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

    @comments = Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day)
    
        10
  •  4
  •   Augustin Riedinger    8 年前

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

      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
      }
    
        11
  •  1
  •   Jenorish    10 年前

    你可以用下面的宝石找到日期之间的记录,

    By star 我使用这个gem和API更加清晰,文档也得到了很好的解释。

    Post.between_times(Time.zone.now - 3.hours,  # all posts in last 3 hours
                      Time.zone.now)
    

    在这里你也可以通过我们的场地 Post.by_month("January", field: :updated_at)

    请查看文档并试用。