在我的数据库中,我有一堆列表,其中包含不同的城市名称。我试图只保留这个数据库中具有城市名称= 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..............))
我做错了什么?
答案 0 :(得分:1)
@unwanted_cities = Listing.where('city != ?', "X")
如果我正在阅读的内容是正确的,那么您希望城市没有名称&#34; X&#34;。您可以通过执行以上代码行来获得这些城市。
如果你想&#34;删除&#34;所有这些城市,
@unwanted_cities.delete_all
或
@unwanted_cities.destroy_all