Rails Tip #06 - Performing calculations

in #ruby6 years ago (edited)

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

Sort:  

@bilal-haider that's great you provide solutions to every big and small problem's. i like this code and i will definitely try it. Keep posting.
you are a good programmer i can imagine what you do with most of your time but that's great.

:D that's exactly what i do :D

This is very lovely. I love this code has it is able to solve problem for adding large amount of money. Nice work!

I am glad you loved the code :)

I already follow and vote for you, are you willing to follow and vote me back