Comparing identical DateTime objects in ruby -- why are these two DateTime.now's not equal?
Possible clue; the act of saving and reloading truncates the seconds_fraction
part of the DateTime. The date field becomes an instance of ActiveSupport::TimeWithZone
. Just saving without reloading doesn't do this; the real DateTime object is still there.
2.0.0-p195 :001 > dt = DateTest.create
2.0.0-p195 :002 > right_now = DateTime.now
2.0.0-p195 :004 > dt.created_at = right_now
2.0.0-p195 :005 > dt.created_at == right_now
=> true
2.0.0-p195 :006 > dt.save
2.0.0-p195 :007 > dt.created_at == right_now
=> true
2.0.0-p195 :008 > dt = DateTest.find(dt.id)
2.0.0-p195 :009 > dt.created_at == right_now
=> false
Edit: of course calling models.each
is going to load the models there and then because of the lazy loading behaviour. Props to the other answerer. As an experiment, try setting models
to Model.where(resource: resource).to_a
.
Compare datetime objects in Rails
Operator ===
is defined in Date class in Ruby which is parent class of DateTime. Operator ===
Returns true if they are the same day which is the case in your example. See description
Now to answer other part of your question about comparing two DateTime, see this answer. Which states that the act of saving and reloading datetime in database truncates the seconds_fraction part of the DateTime.
You can verify that in your rails console by below code -
obj1 = Foo.order("created_at").last.created_at.to_datetime
obj2 = DateTime.parse("2014-09-14 01:12:03 +0200")
obj1.sec > obj2.sec #comparing seconds, returns false. Both equal
obj1.sec_fraction > obj2.sec_fraction #should return true. Not equal.
Comparing dates in rails
Yes, you can use comparison operators to compare dates e.g.:
irb(main):018:0> yesterday = Date.new(2009,6,13)
=> #<Date: 4909991/2,0,2299161>
irb(main):019:0> Date.today > yesterday
=> true
But are you trying to compare a date to a datetime?
If that's the case, you'll want to convert the datetime to a date then do the comparison.
I hope this helps.
Ruby Comparing Time objects considers date. How to avoid that?
You can just modulus the days off to get the remaining time value.
t1 = Time.now
t2 = t2 = Time.at(Time.now.to_i - (5*24*3600)) # later time in day but previous date
tod1 = t.to_i % (24*3600)
=> 69734
tod2 = t2.to_i % (24*3600)
=> 69912
we can clearly see that t2 is a later time of day or clock time if you will and the modulus operation is very clear if you know anything about the unix epoch.
Rails 3, comparing only the dates of two datetime columns in rails
Declare a new attribute that contains just the date:
class WhateverModel < ActiveRecord::Base
...
def viewed_date_only
viewed_date.to_date
end
end
Use that for your comparison in the controller or wherever.
How rails compare with datetime with timestamp
So far as I understand you should do like this (to compare an integer with DateTime convert it to integer also)-
if user.created_at.to_i >= 1563724800
do sth
end
Or you should compare reversely (convert an integer to DateTime)
if user.created_at >= Time.at(1563724800)
do sth
end
time.to_i => int time as an integer number of seconds since epoch.
Time.at(seconds) => time => new time object with the given number of seconds
Related Topics
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
How to Include Ё in [А-Я] Regexp Char Interval
Building a Simple Search Form in Rails
Can't Setup Ruby Environment - Installing Fii Gem Error
Why Is 032 Different Than 32 in Ruby
Show Markers on Google Maps Dynamically -Rails 3.2
Validate That String Contains Only Allowed Characters in Ruby
Parsing Large Xml with Nokogiri
How to Create an Operator for Deep Copy/Cloning of Objects in Ruby
Does Anyone Have Parsing Rules for the Notepad++ Function List Plugin for Ruby and Rake
How to Simplify or Clean Up This Anagram Method
Error:'Incompatible Library Version' SQLite3-1.3.11 in Rails
Rails Not Rolling Back Transaction After Failed Save()
Rails Activeadmin Drop Down Menu on New and Edit Forms
Ruby + Tk Command Binding - Scope Issue
Bundle Install' Fails (Because of Git Protocol)
Ruby: Automatically Set Instance Variable as Method Argument