I have the following query
@initial_matches = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ?", current_user.industry])
Is there a way I can run another SQL query on the selection from the above query using a each do? I want to run geokit calculations to eliminate certain listings that are outside of a specified distance…
Your question is slightly confusing. Do you want to use each..do (ruby) to do the filtering. Or do you want to use a sql query. Here is how you can let the ruby process do the filtering
If you wanted to use sql you could simply add additional sql (maybe a sub-select) it into your
Listing.find_by_sqlcall.If you want to do as you say in your comment.
You are mixing ruby (
location1.distance_from(location2, :units=>:miles)) and sql (WHERE X > 50). This is difficult, but not impossible.However, if you have to do the distance calculation in ruby already, why not do the filtering there as well. So in the spirit of my first example.
This will iterate over all listings, keeping only those that are further than 50 from some predetermined listing.
EDIT: If this logic is done in the controller you need to assign to
@refined_listinstead ofrefined_listsince only controller instance variables (as opposed to local ones) are accessible to the view.