I have some code like:
if @sort_by == 'distance'
Opportunity.upcoming_unsorted \
.is_geocoded \
.near([@query_location.latitude, @query_location.longitude], MilesRadius) \
.paginate(:page => @page, :per_page => PerPage)
else # Opp. date
Opportunity.upcoming_unsorted \
.is_geocoded \
.near([@query_location.latitude, @query_location.longitude], MilesRadius, :order => false) \
.order('opportunity_date') \
.paginate(:page => @page, :per_page => PerPage)
end
I was wondering if it's possible to do this in one rails model search instead of putting the conditional on the if @sort_by.
Basically, I'm giving the user the option to sort by geographic distance (powered by geocoder gem), or 'opportunity_date' field on the model. And want to know if this code can be simplified somehow using rails 5.0. Note: geocoder provides a model .distance field in rails ORM but it's not in the database itself.