This happens if you do it to any month that doesn’t have 31 days (i.e. 3 months would work just fine, but 1 month, or 6, or 8 would all make this happen).
If you do Date.today - 1.month
, it looks like Rails sees that 9/31/2011 isn’t a valid date, so it kicks it back an extra day to make it a valid date. However, when you go one month forward from the end of September, it’ll leave it at 10/30/2011 since that’s a valid date. Basically, Rails just tries to increment (or decrement) the month field and as long as it’s a valid date, it won’t adjust the day field.
One way to work around this is to use the .end_of_month
(or .beginning_of_month
) method on a Date
object in Rails to make sure you’re consistently getting the end or beginning of a month.