Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 带有Arel的Rails作用域,NOW()在迁移后返回NULL_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 带有Arel的Rails作用域,NOW()在迁移后返回NULL

Ruby on rails 带有Arel的Rails作用域,NOW()在迁移后返回NULL,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我已将我的应用程序迁移到Rails 6.0 我有这个范围 scope :published, -> { where(state: 'published').where arel_table[:published_at].lteq 'NOW()' } 这是activerecord在Content.published上生成的查询 SELECT "contents".* FROM "contents" WHERE "contents"."state" = $1 AND "cont

我已将我的应用程序迁移到Rails 6.0

我有这个范围

  scope :published,      -> { where(state: 'published').where arel_table[:published_at].lteq 'NOW()' }
这是activerecord在
Content.published上生成的查询

SELECT "contents".* FROM "contents" WHERE "contents"."state" = $1 AND "contents"."published_at" <= NULL ORDER BY "contents"."published_at" DESC  [["state", "published"]]
=> []
从“内容”中选择“内容”。*其中“内容”。“状态”=$1和“内容”。“发布位置”[]

为什么
NOW()
NULL

重写此查询以使用文本字符串作为WHERE条件:

scope:published,->{where(state:'published')。where('published_at{where(state:'published')。where('published_at不为NULL');
(也许你的设计就是这样的?)


希望对您有所帮助!:)

重写此查询,以便为WHERE条件使用文字字符串:

scope:published,->{where(state:'published')。where('published_at{where(state:'published')。where('published_at不为NULL');
(也许你的设计就是这样的?)


希望有帮助!:)

使用一个符号告诉arel,您不仅仅是在向查询中传递危险的字符串,而是确保在查询中打印是安全的

->{where(state:'published')。where arel_table[:published_at]。lteq:'NOW()}

使用一个符号告诉arel,您不仅仅是在向查询中传递危险的字符串,而是确保在查询中打印是安全的

->{where(state:'published')。where arel_table[:published_at]。lteq:'NOW()}

不,他们仍然存在你是对的-我编辑了我的答案,没有关于arel_表的第一个注释。希望这有帮助:)不,他们仍然存在你是对的-我编辑了我的答案,没有关于arel_表的第一个注释。希望这有帮助:)