开发者

Group same elements together and append default values if not found

开发者 https://www.devze.com 2023-03-14 18:24 出处:网络
thanks for the taking the time to read my question. I\'m pulling information for purposes of calculation:

thanks for the taking the time to read my question.

I'm pulling information for purposes of calculation:

...collect{|x| [x.calc_year,x.calc_month, x.amount]

Now I want to 开发者_如何学Cbe able to put all the 2010, 2011 values in different arrays so I can easily fetch the values for the months.

for example year["2011"][1] or year["2010"][0] ..

And my second question is, lets say I dont have any information for the 5th month, I want to be able to enter 0 for the amount and enter the 5th month detail.

thank you


You can use group_by

@information.group_by(&:calc_year).each do |year, calcs_for_year|

For the second question, you can loop through and first write blanks for all the years since the previous year.

@information.group_by(&:calc_year).each do |year, this_years_info|

    unless previous_year.nil? # first time ignore

      (year - previous_year - 1).times do |i| 
          blank_year = previous_year + i
          # render your empty year figures/form
      end

    end

    # render year figures/form

    previous_year = year
end   
0

精彩评论

暂无评论...
验证码 换一张
取 消