1

My view lists a set of expenses, I would like to sum the values in the current view. This is the code I am trying in my controller.

@project = Project.find(params[:project_id])
@expense_list = @project.expenses.find_all_by_user_id(current_user.id).sort_by(&:expense_date)
@expense_total = @expense_list.sum(:amount)

The error I get is:

undefined method `+' for #<Expense:0x007f907a2ba888>

Any advice? I feel like I'm doing something wrong. Thanks!

Atari2600
  • 1,219
  • 2
  • 13
  • 26

1 Answers1

1

Try this:

@expense_total = @project.expenses.where(:user_id => current_user.id).sum(:amount)
Emu
  • 5,763
  • 3
  • 31
  • 51