Reputation: 1977
I do:
Order.find(:all, :conditions => "created_at>='#{DateTime.now.year}-#{month}-1' and created_at<='#{Date.new(DateTime.now.year, month.to_i, -1)}'")
That is working fine except that everything created on the last day of the month is not included. Created_at might contain: "2010-09-30 18:34:09". That is NOT less than or equal to "2010-09-30". I know that I could just change the method to this:
Order.find(:all, :conditions => "created_at>='#{DateTime.now.year}-#{month}-1' and created_at<='#{Date.new(DateTime.now.year, month.to_i, -1)} 23:59:59'")
...but that seems kind of wrong. Is there a smarter way to do it?
Upvotes: 0
Views: 4719
Reputation: 1691
Or you could use
< Date.today.next_month.beginning_of_month
to get everything less than the next month.
Upvotes: 2
Reputation: 15552
This might not solve your issue but you should make use of Rails' Ruby extensions here. You can do things like 1.month.ago
which will return a DateTime object representing the current date minus 1 month.
Other methods you might expect are defined as well. 4.days.from_now
, 3.days.since(Date.yesterday)
, etc.
Upvotes: 0
Reputation: 21892
That would be the right way to do it, IMHO. The representation of a time as 2010-09-30 18:34:09
is conceptually just a human-friendly display of a single number. So think of it as doing comparisons against a number. Therefore it makes sense to be explicit about the minimum and maximum values of the number in question.
Upvotes: 0