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

后端 未结 2 1493
醉话见心
醉话见心 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: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)

提交回复
热议问题