Side Note: The original version of this answer was posted before we determined that thinking-sphinx was being used, so it is important to note that I have never used this gem / library, and can only offer my best estimated guess based on the documentation.
The error message is saying you don't have a method or variable called zip_codes
, which from what we can see in the code, is correct (it is most definitely not defined anywhere).
If I am correct that thinking-sphinx returns an array of Active Record objects (or something similar), then I believe you should be able to use most of this code (using the pluck
method below).
Rather than using the search_for_ids
method, which I believe only returns a collection of database row ids, we'll use the normal search
method. As my understanding goes, what is returned from the search
method is a collection of fully populated active record objects. I believe your location search will look like this:
locations = Location.search
:geo => [latitude, longitude],
:with => {:geodist => 0.0..400_000.0},
:order => 'geodist ASC',
:per_page => 1_000
You should be safe, at that point, to extract only the zipcode
fields from that collection, like so:
User.where(zip_code: locations.pluck(:zipcode))
If the pluck
method does not exist for that collection, you may need to try the same procedure with the map
method instead:
locations.map(&:zipcode)
Or a more explicit version:
locations.map{|location| location.zipcode}
The reason why your attempt to do .where(:zip_code => zip_codes)
after renaming the variable is because (most likely) your zip_codes
(previously named location_zips
) is an array of objects, not a collection of zipcodes as you expected. Admittedly, this is an assumption because I do not know what this search_by_zipcode
method does, but this seems like a reasonable explanation based on the behaviour you described.