0

According to Indian financial year(April-1 to March-31), how do i get range when month changed to January my logic fails after 31 December, nothing is displayed.Here is my code for displaying all months: Model:

def self.get_month(month)
    months={
       '1'=> 'January',
       '2'=> 'February',      
       '3'=> 'March',
       '4'=> 'April',
       '5'=> 'May',
       '6'=> 'June',
       '7'=> 'July',
       '8'=> 'August',
       '9'=> 'September',
       '10'=> 'October',
       '11'=> 'November',
       '12' => 'December'
      }
   months[month]
 end 

views:

<% (current_financial_year.start_date.month..Date.today.month).times do |a| %>
             <tr>
               <td><%= PayrollDetails.get_month(a.to_s)%></td>
             </tr>
             <% end %>

This code works well up to December month but in January when year changed it displays nothing, i got this error:

undefined method `times' for 4..1:Range

how do i fix this revers range problem in rails3, thanks in advance.

Ravindra
  • 1,039
  • 2
  • 12
  • 26

2 Answers2

1

It seems you're not handling the wrap case. There are many ways you could go about handling it. This is the most basic way I can think up:

<% (current_financial_year.start_date.month..(Date.today.month < current_financial_year.start_date.month ? Date.today.month+12 : Date.today.month)).times do |a| %>
  <tr>
    <td><%= PayrollDetails.get_month((a%12).to_s)%></td>
  </tr>
<% end %>

This is rather ugly. You might want to create a local variable for current_month or you could open up the Date class and define a financial month which does the ? : logic.

I hope that helps.

Geoff
  • 2,208
  • 1
  • 16
  • 17
0

I agree with @Geoff s answer i have changed this little bit to fit in my condition:

 <% for a in (current_financial_year.start_date.month..(Date.today.month < current_financial_year.start_date.month ? Date.today.month+12 : Date.today.month)) %>
             <tr>
               <td><%= PayrollDetail.get_month((a>12? (a -12) : a ).to_s)%></td>
<% end %>

And this works for me.

Ravindra
  • 1,039
  • 2
  • 12
  • 26