views:

43

answers:

1

I would like to make two drop downs. a start time, and an end time. Specifically, I only need months. I would like to, for example, choose January, and then March, and then have the database read that it is the these two months plus February.

Is there any out of the box migration that could work?

I'm guessing..

script/generate migration AddMonthsToClass beginDate:datetime #through endDate:datetime

I apologize ahead of time if my question sounds retarded! Sorry! :D

+1  A: 

This may help you: http://www.francisfish.com/getting_the_number_of_months_between_two_dates_in_rubyrails.htm

After you get the number of months, you can get the month names from this:

months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
months[start_date.month, number_of_months]

eg
months[Time.now.month, 5] => ["May", "June", "July", "August", "September"]

edit:
Instead of that, you can use:

Date::MONTHNAMES[start_date.month, number_of_months]

edit:
Or if you get the month numbers in your select tag, you can just use:

Date::MONTHNAMES[start_month_num, end_month_num]

but, this would fail if end_month_num is less than start_month_num

This should work:

if start_month <= end_month
  Date::MONTHNAMES[start_month..end_month]
else
  Date::MONTHNAMES[end_month..12] + Date::MONTHNAMES[1..start_month]
end
Kalyan M
Excellent response! I can't thank you enough!
Trip