Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 比较sql请求和Time.now.utc的日期时间_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails 比较sql请求和Time.now.utc的日期时间

Ruby on rails 比较sql请求和Time.now.utc的日期时间,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我有一个jsonb字段,在其中存储令牌过期日期,如下所示: user.token = {token_expires: Time.now.utc} user.save User.where("""(users.token ->> 'token_expires')::timestamp <= :current_time""", current_time: Time.now.utc).pluck(:id)

我有一个jsonb字段,在其中存储令牌过期日期,如下所示:

user.token = {token_expires: Time.now.utc}
user.save
User.where("""(users.token ->> 'token_expires')::timestamp <= :current_time""",
                                       current_time: Time.now.utc).pluck(:id)
当我这样做的时候

user.token['token_expires']
=> "2018-09-23T18:21:58.010Z"
结果是2018-09-23T18:21:58.010Z

如果我在控制台中检查当前日期时间

Time.now.utc
=> 2018-09-23 19:06:20 UTC
结果是2018-09-23 19:06:20 UTC

现在的问题是当我尝试运行以下请求时:

User.where("""users.token ->> 'token_expires' <= :current_time""",
                                           current_time: Time.now.utc).pluck(:id)
=> []  # returns an empty array/result

User.where(““”users.token->>”token\u expires“我将在这里回答我的问题,希望有人能发现它有用

每个人都需要了解的是,date或datetime以字符串形式存储在json/jsonb字段中,通常情况下,我的代码会发生什么

user.token = {token_expires: Time.now.utc}
user.save
Rails隐式地
Time.now.utc
转换为iso8601,并将其存储为字符串,这就是我这样做的原因(在控制台中)

我得到了“2018-09-23T18:21:58.010Z”
,这是我上一次使用的iso8601格式

我不打算解释Rails为什么要进行这种转换,但据我所知,建议使用ISO-8601格式在数据库中存储日期/日期时间,特别是对于json/jsonb字段,因为这样可以方便地处理日期或使用API

除了ISO-8601之外,您还可以存储其他格式吗? 是的,您可以,这取决于您的需要。例如,我可以通过在代码中传递字符串而不是像这样的日期时间来阻止Rails为我执行隐式转换:

user.token = {token_expires: Time.now.utc.to_s}
user.save
存储的日期时间(字符串)如下所示:2018-09-23 18:21:58 UTC

您可能希望将日期存储为unix时间,这取决于您

回到我的问题和没有返回任何内容的查询,我的查询如下:

User.where("""users.token ->> 'token_expires' <= :current_time""",
                                       current_time: Time.now.utc).pluck(:id)
以下是帮助我得出这个结论的其他相关问题:


从日期时间检查中删除时区
User.where("""(users.token ->> 'token_expires')::timestamp <= :current_time""",
                                       current_time: Time.now.utc).pluck(:id)