Skip to content

Using includes in query removes distance method #33

@mquan

Description

@mquan
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions