Reputation: 36
I'm having a rough time scratching my head with this one. I have A task class. Tasks can be repeated each day of the week. I have a handlers class that are assigned tasks. I'm currently working on a dashboard to show handlers what tasks they have today depending on the day of the week.
In my handler model:
def todays_walks
# Sunday
if Time.now.wday == 0
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_sunday = ?', id, Time.now.beginning_of_day, true)
end
# Monday
if Time.now.wday == 1
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_monday = ?', id, Time.now.beginning_of_day, true)
end
# Tuesday
if Time.now.wday == 2
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_tuesday = ?', id, Time.now.beginning_of_day, true)
end
# Wednesday
if Time.now.wday == 3
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_wednesday = ?', id, Time.now.beginning_of_day, true)
end
# Thursday
if Time.now.wday == 4
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_thursday = ?', id, Time.now.beginning_of_day, true)
end
# Friday
if Time.now.wday == 5
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_friday = ?', id, Time.now.beginning_of_day, true)
end
# Saturday
if Time.now.wday == 6
Task.where('handler_id = ? AND start_time >= ? AND tasktype_id = 1 AND repeat_saturday = ?', id, Time.now.beginning_of_day, true)
end
end
Then in my view:
<h2>Your Day</h2>
<h3>Pups to walk:</h3>
<% if @handler.todays_walks.empty? %>
<p>You have no dogs to walk today.</p>
<% else %>
<%= render :partial => 'admin_shared/dashboard_pup', :collection => @handler.todays_walks.pup, :as => :pup %>
<% end %>
If I force any day of the week except for Sunday, it works. I imagine that this is somehow because Sunday evaluates to 0? I can't figure it out.
Upvotes: 0
Views: 60
Reputation: 151
I'm not sure why this doesn't work for you, but I would start from a quick refactoring to make this method much more DRY. Try this:
def todays_walks
days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]
return Task.where("handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_#{days[Time.now.wday]} = ?", id, Time.now.beginning_of_day, true)
end
Upvotes: 1
Reputation: 36
I figured it out... Just a 'duh' moment. The model wasn't actually returning the objects. It worked on other days because they had repeat tasks.
So to fix, in the models:
if Time.now.wday == 0
return Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_sunday = ?', id, Time.now.beginning_of_day, true)
end
Upvotes: 0