activerecord-postgres-earthdistance icon indicating copy to clipboard operation
activerecord-postgres-earthdistance copied to clipboard

Using `includes` in query removes distance method

Open mquan opened this issue 7 years ago • 0 comments

class Company < ApplicationRecord
  has_many :addresses
  acts_as_geolocated through: :addresses
end

class Address < ApplicationRecord
  acts_as_geolocated lat: 'latitude', lng: 'longitude'
  belongs_to :company 
end

> Company.within_radius(20_000, *point).selecting_distance_from(*point).first.distance
=> 11081.8179360831

> Company.includes(:addresses).within_radius(20_000, *point).selecting_distance_from(*point).first.distance

NoMethodError: undefined method `distance' for #<Company:0x00007fbbd7584888>

Separately, just wonder if it's possible to query Company but have the distance method attached to the the addresses?

mquan avatar Apr 03 '18 16:04 mquan