Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby ActiveRecord Basics Migrations and Relationships Looking at our Application

How the total cost is calculated?

Hi Im trying to understand the project that is in the video, and in the line 4 of accounts/show.html.erb

Total Cost: <%= (@account.account_entries.map &:cost).sum %>

Im really lost with the way that total cost is calculated I do not understand the .map neither the &:cost and what is returned within the pharentesis? (...).sum

Thanks for your explanation

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

.map will go through every account_entry for the given account, take the cost attribute, return an array of all these values and then .sum will add all of them together and return the final result. I believe that &:cost tells map to only look at the cost attribute an dignore all other things.