0
votes

I have an array

@array = [{:start_date=>"13/10/2020", :amount=>32130, :hours=>18, :field=>"blabla", :user_id=>123456}, 
 {:start_date=>"06/10/2020", :amount=>12355, :hours=>18, :field=>"blabla", :user_id=>3211},
 {:start_date=>"02/09/2020", :amount=>55222, :hours=>18, :field=>"blabla", :user_id=>24457},
 {:start_date=>"14/08/2020", :amount=>22565, :hours=>18, :field=>"blabla", :user_id=>24457},
 {:start_date=>"13/08/2020", :amount=>454454, :hours=>18, :field=>"blabla", :user_id=>24457},}]

I want to group_by month (start_date) to have something like this : October :

    13/10/2020 amount=>32130, hours=>18, field=>"blabla", user_id=>123456}
    06/10/2020", :amount=>12355, :hours=>18, :field=>"blabla", :user_id=>3211

September:

    02/09/2020", :amount=>55222, :hours=>18, :field=>"blabla", :user_id=>24457

August

    14/08/2020", :amount=>22565, :hours=>18, :field=>"blabla", :user_id=>24457
    13/08/2020", :amount=>454454, :hours=>18, :field=>"blabla", :user_id=>24457

I tried:

@array.group_by { |t| (t[:start_date].to_date).strftime('%B %Y') }

but it did not works (the array is not group by) How can i make this grouping plz ? Thanks

1

1 Answers

0
votes
pry(main)> @array.group_by { |t| Date.strptime(t[:start_date], '%d/%m/%Y').strftime('%B') }
=> {"October"=>
  [{:start_date=>"13/10/2020", :amount=>32130, :hours=>18, :field=>"blabla", :user_id=>123456},
   {:start_date=>"06/10/2020", :amount=>12355, :hours=>18, :field=>"blabla", :user_id=>3211}],
 "September"=>[{:start_date=>"02/09/2020", :amount=>55222, :hours=>18, :field=>"blabla", :user_id=>24457}],
 "August"=>
  [{:start_date=>"14/08/2020", :amount=>22565, :hours=>18, :field=>"blabla", :user_id=>24457},
   {:start_date=>"13/08/2020", :amount=>454454, :hours=>18, :field=>"blabla", :user_id=>24457}]}