简体   繁体   English

如何在Rails中获得where id => id集合的逆函数

[英]How to get the inverse of where ids => collection of ids in Rails

I'm trying to return the inverse of this statement: 我正在尝试返回以下语句的反函数:

scope :subscribed, lambda {|user| where(:venue_id => user.flagged_venues)}

flagged_venues returns an array of ids, so when I run that statement I get all of the flagged venues. flagged_venues返回一个ID数组,因此当我运行该语句时,我将获得所有标记的场地。 What I actually want is the exact opposite: all the venues except for the flagged ones. 我真正想要的是完全相反的东西:除了标记的场地以外的所有场地。

I tried it with != instead of => but that returned all the venues. 我用!=而不是=>尝试,但是返回了所有场地。

When it's changed to where("venue_id <> ?", user.flagged_venues) then I get this error: PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type record LINE 1: SELECT "events".* FROM "events" WHERE (venue_id <> 2,4) 当将其更改为where("venue_id <> ?", user.flagged_venues)以下错误: PG :: DatatypeMismatch:错误:WHERE的参数必须为布尔值类型,而不是记录类型LINE 1:选择“事件”。*在“事件”中的位置(venue_id <> 2,4)

You want the ids that are not in the list of flagged_venues , so my guess is that you should replace 您想要的ID不在flagged_venues列表中,因此我的猜测是您应该替换

where(:venue_id => user.flagged_venues)

by 通过

where("venue_id not in (?)", user.flagged_venues)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM