I have many multi-line ActiveRelation query methods in our app, and I'm unsure about the most idiomatic way to write these methods. Take a look at this example:
def postal_code_ids_within(miles)
nearby_postal_codes = PostalCode.where("latitude > :min_lat and latitude < :max_lat",
min_lat: (latitude - (miles.to_f / MILES_PER_DEGREE_LATITUDE.to_f / 2.to_f)),
max_lat: (latitude + (miles.to_f / MILES_PER_DEGREE_LATITUDE.to_f / 2.to_f)))
nearby_postal_codes = nearby_postal_codes.where("longitude > :min_lon and longitude < :max_lon",
min_lon: (longitude - (miles.to_f / MILES_PER_DEGREE_LONGITUDE.to_f / 2.to_f)),
max_lon: (longitude + (miles.to_f / MILES_PER_DEGREE_LONGITUDE.to_f / 2.to_f)))
nearby_postal_codes.pluck(:id)
end
It feels a bit off to me. A block from which an ActiveRelation object is returned seems idiomatic, but I haven't seen that approach around.
What is standard?
Building on Brian's suggestion, this is much more legible and works well.
scope :near, lambda { |postal_code, miles|
degree_offset = miles / MILES_PER_DEGREE / 2
where("latitude > :min_lat and latitude < :max_lat and longitude > :min_lon and longitude < :max_lon",
min_lat: postal_code.latitude - degree_offset,
max_lat: postal_code.latitude + degree_offset,
min_lon: postal_code.longitude - degree_offset,
max_lon: postal_code.longitude + degree_offset)
}
def postal_code_ids_within(miles)
self.class.near(self, miles).pluck(:id)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With