Just wondering if there is any function that allows to do that:
MyModel.find_by_conditon(method_a - method_b > 0)
MyModel.class
def method_a next_service_date.to_i end def method_b last_service_date.to_i end
def next_service_date
service_date.present? ? calculated_time_estimation : row_time_estimation
end
def calculated_time_estimation
service_date + calculated_service_period
end
def calculated_service_period
case(service_frequency_period)
when 'Year'
service_frequency_number.to_i.year
when 'Month'
service_frequency_number.to_i.month
when 'Week'
service_frequency_number.to_i.day * 7
when 'Day'
service_frequency_number.to_i.day
end
end
service_frequency_number, service_frequency_period, service_date are attributes for MyModel
Assuming that
method_aandmethod_bare actually attributes, you can do:Edit
I think the only way that you’re going to be able to query on a calculated field is to move the calculation to the DB, where you would have a scalar function to return the results. Then you could do
MyModel.where('next_service_date_on_db(service_date) > 0'). Unfortunately, that would tie you to a DB-specific implementation, but you’re not going to be able to query that way without a server-side function.Now, if you have the entire collection, you could filter based on those conditions, but you’ll have to load the entire set. For example:
#allreturns an array of all of the objects, and select filters based on the condition of the block. Unfortunately, this solution loads all of the records to sort application side.