Ruby on rails 带有Arel的Rails作用域,NOW()在迁移后返回NULL
我已将我的应用程序迁移到Rails 6.0 我有这个范围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
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_表的第一个注释。希望这有帮助:)