我想要保留一系列ID,然后删除其他ID,我们有3种类型的用户,Admins
,Investors
和Students
。我的列表是Students
id' s,所以我想删除所有内容。
如果我试试这个
User.where.not(id: GOOD_LIST).count(:all)
我将获得15k记录。但如果我尝试添加一个额外的条件
User.where.not(id: GOOD_LIST).where(type: "Student").count(:all)
or
User.where.not('id IN ? AND type = ?', GOOD_LIST, "Student").count(:all)
它将返回0条记录。
我尝试使用where方法
User.where('id NOT IN ?', GOOD_LIST).where(type: "Student").count(:all)
但收到以下错误消息
(10.7ms) SELECT COUNT(*) FROM "users" WHERE "users"."type" = 'Student' AND (id NOT IN 39999,40000,40001,40002,40003,40004,40005,40006,40007,40008)
PG::SyntaxError: ERROR: syntax error at or near "39999"
LINE 1: ... WHERE "users"."type" = 'student' AND (id NOT IN 39999,4000...
这样做的最佳方式是什么?
答案 0 :(得分:1)
生成的SQL缺少列表周围的括号。所以我们应该在你的模板中添加它们,比如这个
.where('id NOT IN (?)', GOOD_LIST)