以下是代码段
Wip.where(status: "P").where("reason NOT IN ?", ['DOB_MISSING'])
这不起作用。
我试过寻找它并发现了很多问题。他们中的一些人建议这样做,但我不确定我哪里出错了
这里我粘贴了一部分我的导轨c
[19] pry(main)> Wip.where(status: "P").where("reason NOT IN ?", ['DOB_MISSING']).count
(0.6ms) SELECT COUNT(*) FROM `wip` WHERE `wip`.`status` = 'P' AND (reason NOT IN 'DOB_MISSING')
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''DOB_MISSING')' at line 1: SELECT COUNT(*) FROM `wip` WHERE `wip`.`status` = 'P' AND (reason NOT IN 'DOB_MISSING')
from /home/ritu/.rvm/gems/ruby-2.2.3@svasth/gems/rack-mini-profiler-0.9.7/lib/patches/db/mysql2.rb:20:in `query'
答案 0 :(得分:2)
这是一个简单的语法错误问题:
where("reason NOT IN (?)", ['DOB_MISSING'])
所有IN
条款都需要用方括号括起来。
答案 1 :(得分:0)
您也可以使用arel_table
方法:
restrict_arr = ['DOB_MISSING']
Wip.where(status: "P").where(Wip.arel_table[:reason].not_in(restrict_arr))