Hi I am having issues comparing datetimes with a Rails 4 JSON API.
I use a scope with the created_at attribute
scope :later_than, -> (date) { where('created_at > ?', date) }
The JSON returned for the created_at looks like
"created_at" = "2015-05-07T01:16:43.611Z"
When sending this same datetime the API log and SQL look like
Parameters: {"later_than"=>"2015-05-06T21:16:43.611-0400"}
created_at > '2015-05-06T21:16:43.611-0400'
In the console
Object.first.created_at
=> Thu, 07 May 2015 01:16:43 UTC +00:00
Object.first.created_at > '2015-05-06T21:16:43.611-0400'
=> true
The last comparison should be false, but instead it is true and the scope is not working properly.
When logging
Object.first.created_at - '2015-05-06T21:16:43.611-0400'.to_datetime
=> 0.000402
So what happens is:
Thus when sending the same datetime to the API, the microseconds will be set to 0 and thus the comparison is not going to work as expected.
Is there a way to tell Rails: compare datetimes with millisecond precision?
Is Message.first.created_at
equal to Thu, 07 May 2015 01:16:43 UTC +00:00
? (you have Object.first.created_at
up there) because if it is, the console expression is behaving correctly and should be true.
你尝试过这样的事情吗?
Object.first.created_at > Time.parse('2015-05-06T21:16:43.611-0400')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.