banditKing
banditKing

Reputation: 9579

How to make this Active record query work

In my DB I have a bunch of listings with different city names. Im trying to keep only those listings in this db that have city name = X and remove all other cities.

Im trying the following Active record Query:

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

But Im getting this error

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..............))

What am I doing wrong?

Upvotes: 0

Views: 95

Answers (1)

varatis
varatis

Reputation: 14750

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

If what I'm reading is correct, you want cities that don't have the name "X". You'll get these cities by executing the above line of code.

If you want to "remove" all of these cities,

@unwanted_cities.delete_all

or

@unwanted_cities.destroy_all

Upvotes: 1

Related Questions