What's the idiomatic way to write multi-line ActiveRelation queries?

Viewed 6838

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?

2 Answers
Related