Rails Tip #06 - Performing calculations

consider a scenario, where you have balances in a wallet and you want to sum all of them.

Where we have a wallet model, that contains a balance column

With this simple code, we can sum all the user's balance. it can help us, to see how much total worth our company has.

Wallet.sum(:balance)

if we want to sum all the balance of current user, very easy

current_user.wallets.sum(:balance)

should return us the sum

Here I am doing a similar calculation for vote weight..
Screenshot from 2018-05-02 14-12-37.png

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center