ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Adding hash value pair #830

Closed HabRonan closed 7 years ago

HabRonan commented 7 years ago

For a Crowdfunding application: I am trying to add each donation: I tried this

<% @project.donations.each do |donation| %>
  sum: <%= donations += donation.donation_amt %>
  <% end %>

but, I am getting: "undefined method `+' for nil:NilClass"

amaseda commented 7 years ago

Did you mean to say donation instead of donations inside of the each block?

Otherwise, donations is a collection of Active Record objects, so you can't just add to it since it's not a number.

@HabRonan

HabRonan commented 7 years ago

Hey Adrian, It's working now. Used this instead

<p>Amount raised: <%= @project.donations.pluck(:donation_amt).reduce(:+) %></p>

I forgot donations is a collection, I was actually doing

sum: <%= sums += donation.donation_amt %>

and then I went I can just say donations b.c. I can name whatever I want.

amaseda commented 7 years ago

Wonderful!