Reputation: 5174
I have a model with a datetime
column for projected delivery dates and want to pull records of the orders that will be delivered next week. Anyone know the best way to show this?
model Orders
created_at, datetime
modified_at, datetime
deliver_at, datetime # stores a future date when the package will be delievered
I'm trying to get all the items with deliver_at
projected for next week.
Upvotes: 0
Views: 1909
Reputation: 3534
I prefer this method, as it avoids unnecessary SQL:
Order.where(:deliver_at => Time.now.next_week..Time.now.next_week.end_of_week)
This would find all orders where the deliver_at
date is between the beginning of next Monday and the end of the following Sunday.
I recommend checking out the Rails Guide for the ActiveRecord Query Interface. This method is described in there.
Upvotes: 6
Reputation: 15056
Order.where("deliver_at > ? AND deliver_at < ?", Time.now.next_week, Time.now.next_week.end_of_week)
Upvotes: 0