i have a custom method on my model and i want to use it in a where condition, i already accomplish this for sorting with custom methods, but can't find a way for search with custom conditions.
Now i have this, in my model
attr_accessible :end, :start
def time_left
self.end.to_i - Time.now.utc.to_i - Time.now.utc_offset
end
def time_left_to_start
self.start.to_i - Time.now.utc.to_i - Time.now.utc_offset
end
def status
if self.time_left_to_start > 0
1 #proximamente
elsif self.time_left_to_start < 0 and self.time_left > 0
2 #funcionando
else
3 #finalizado
end
end
and for my controller
def index
@next_events = Event.all.sort_by(&:time_left).reverse!.reject { |e| e.status != 1 }.take(5)
end
i know i can write the entire condition on the where method, but my idea is don't repeat my self and avoid to loop for all the events to remove each one where the status isn't 1, i just need the last 5 each time and looks to me really bad idea ask to the db for all the records each time. Thanks in advance, regards!