ActiveRecord: milliseconds aren't taken into account when using a where clause

后端 未结 2 1473
醉话见心
醉话见心 2021-01-05 07:18

I\'m working on a project with a rails api and an iOS client, using the updated_at field as the reference to detect modifications on the server that happened after the last

相关标签:
2条回答
  • 2021-01-05 08:10

    If you're using the ISO8601 standard you can use .iso8601(10). Don't know why but it rounds to seconds by default.

    0 讨论(0)
  • 2021-01-05 08:13

    Try

    Model.where("updated_at > ?", last_update.strftime("%Y-%m-%dT%H:%M:%S.%N%z"))
    

    You can also set this format as the standard format for DateTime in databases by setting (i.e. in an initiallizer):

    Time::DATE_FORMATS[:db]= '%Y-%m-%dT%H:%M:%S.%N%z'
    

    then your original query works again:

    Model.where("updated_at > ?", last_update)
    

    See the Rails API for DateTime.to_s (aka .to_formated_s)

    0 讨论(0)
提交回复
热议问题