We’re using the geokit-rails
gem to find all locations within a certain distance from another location.
class CreateLocations < ActiveRecord::Migration[7.2]
def change
create_table :locations do |t|
t.decimal :lat
t.decimal :lng
t.float :radius
end
end
end
class Location < ApplicationRecord
acts_as_mappable
end
The following works great and returns all locations within 5 miles of (37,-122)
Location.within(5, origin: [37,-122])
We next need to find all locations within the distance stored in the radius column in Location. The following query does not work.
Location.within('locations.radius', origin: [37,-122])
Can someone help me figure out what I’m doing wrong?
2
Answers
Just use simple location object with radius
After running through the source code a bit as @max pointed out you can use
This will result in
<=
but it is a lot cleaner than the alternativesThe formula differs from yours though.
We can create your formula as follows:
Usage:
Produces