Why does Date.yesterday counts as Date.today also?

前端 未结 2 1056
生来不讨喜
生来不讨喜 2021-01-11 15:51

I have the following model and methods:

class UserPrice < ActiveRecord::Base
  attr_accessible :price,  :purchase_date,    

  def self.today
    where(:p         


        
2条回答
  •  走了就别回头了
    2021-01-11 16:16

    This is happening because calculations.rb is calling the "current" method of the Date class for the configured timezone (Defaults to UTC).

    If you open the rails console you can see the date at which "Date.yesterday" is calculating on by doing:

    Time.zone.today
    

    This will probably show you tomorrow's date. So Date.yesterday for what rails sees as today, is today. ;)

    You can work with the Date library more directly by doing:

    Date.today.advance(:days => -1)
    

    This will give you yesterday's date like you expect whereas active_support is returning:

    Date.current.advance(:days => -1) 
    

提交回复
热议问题