lundi 17 avril 2017

Add sum array to active record object where something equals something

I want to sum my amount column plus display my original activerecord object together where my @val emp_no = @sell_back emp_no. Question is how can I combine the following because my @sell_back returns 4 active record objects and my @val only returns two values because I have only two emp_no with two records each. Any help would be greatly appreciated

Here is what I tried so far

Controller

def main_view
  @sell_back = SellBack.all
  @sell_back_tot = SellBack.all.group('emp_no')
  @val = @sell_back_tot.sum('amount')
end 

Here is my view

  %tr.trace-table
  -@sell_back.each do |sellback|
    -@val.each do |v|
      %tr.trace-table
        %td.trace-table{:style => 'border: solid black;'}
          %span.u= sellback.emp_f.capitalize
          %span.u= sellback.emp_l.capitalize
        %td.trace-table{:style => 'border: solid black;'}= sellback.amount
        %td.trace-table{:style => 'border: solid black;'}= v[1]
        %td.trace-table{:style => 'border: solid black;'}
          %span.u= best_in_place sellback, :status, :as => :select, :collection => ['Approve', 'Disapprove', 'Pending']

This doesn't work because it adds more rows..

Aucun commentaire:

Enregistrer un commentaire