I have 2 related rail models
class Location < ActiveRecord::Base has_many :rates geocoded_by .... end class Rate < ActiveRecord::Base belongs_to :location end
I use a geocoder gem - http://www.rubygeocoder.com
I want to find all tariffs with a specific pair attribute, located at a certain distance from the user.
In SQL, I would make an internal join at rates
SELECT * FROM rates INNER JOIN locations ON rates.location_id=locations.id;
Then insert the where clause to filter the attribute of the pair, and then use the Geocoder method next to the resulting table (the near method works by inserting the where clause into the query, which calculates the distance using latitude and longitude attributes for the location) to select rows only on right distance
How can I do this on rails? I tried
rates = Rate.joins(:locations)
I get
ActiveRecord::ConfigurationError: Association named 'locations' was not found; perhaps you misspelled it?
I want to do it
rates = Rate.joins(:locations).near(my_latitude, my_longitude, distance).where(:rates => {:pair => 'xxxx'})
but i get
undefined method `near' for #<ActiveRecord::Relation:0xa075ff8>
Imme22009
source share