Rails inner join combined with geocoding stone

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> 
+7
source share
2 answers
 near = Location.near(location, radius) Rate.includes(:location).references(:location).merge(near) 

This can be related to other areas of location or speed.

+7
source

I know this is old, but still didn’t answer anywhere :)

I ran into the same problem, then came across a little information in the depth of the geocoder documentation , where he said that in order to do an external join operation (in which it is done with rails on the rails), you should use the combination in conjunction with: select and for him (I needed to find all the products from users in a specific place).

So, just from my head, this might work in your case:

 rates = Location.near(my_latitude, my_longitude, distance, :select => "rates.*").joins(:locations).where(:rates => {:pair => 'xxxx'}) 

Hope this helps.

+2
source

All Articles