0

在我的数据库中,我有一堆不同城市名称的列表。我试图仅保留此数据库中具有城市名称 = X 的列表并删除所有其他城市。

我正在尝试以下活动记录查询:

@unwanted_cities = Listing.where('city not in (?)', Listing.where('city = ?', "X"));

但我收到这个错误

ActiveRecord::StatementInvalid in Unwanted_cities#index

Showing /Users/AM/Documents/RailsWS/app0521/app/views/unwanted_cities/index.html.erb where     line #31 raised:

PG::Error: ERROR:  operator does not exist: character varying <> integer
LINE 1: SELECT "listings".* FROM "listings"  WHERE (city not in (4,1...
                                                     ^
HINT:  No operator matches the given name and argument type(s). You might need to add  explicit type casts.
: SELECT "listings".* FROM "listings"  WHERE (city not in         (4,10,13,17,18,19,21,23,26,28,29,31,36,39,46,48,49,52,90,97,101,103,105,108,109,111,115,94,5      ,219..............))

我究竟做错了什么?

4

1 回答 1

1

@unwanted_cities = Listing.where('city != ?', "X")

如果我正在阅读的内容是正确的,那么您需要没有名称“X”的城市。您将通过执行上述代码行获得这些城市。

如果你想“移除”所有这些城市,

@unwanted_cities.delete_all

或者

@unwanted_cities.destroy_all

于 2012-06-30T16:07:49.047 回答