Hello everyone.
I have this piece of code in a model that sets a start date and end date according to the time of the day that the method is run.
Let's jump right into the code example:
#MODEL
now = Time.now
if now.hour >= 17 && now.hour <= 23
#night
n = now+1.day
startd = Time.local(now.year, now.month, now.day, 17, 00, 00)
endd = Time.local(n.year, n.month, n.day, 8, 00, 00)
elsif now.hour >= 0 && now.hour <= 7
#morning
n = now-1.day
startd = Time.local(n.year, n.month, n.day, 8, 00, 00)
endd = Time.local(now.year, now.month, now.day, 17, 00, 00)
end
Then inside a model (same or another), I am trying to run this function inside a find.
#MODEL(SAME OR OTHER)
Model.find(:all,
:conditions => ['created_at >= ? AND created_at <= ?', startd, endd])
The problem is that I'm going to use that function to set the start and end dates a lot, and I dont know where to place it to not repeat myself. It's going to be used from different models.
Thanks for any help.