How and When to Use Money Rails gem

Nick Silas
3 min readJan 25, 2021

When getting comfortable with Ruby on Rails, the first instinct seems to be to download gems for everything. Don’t get me wrong, gems are super helpful when used appropriately. The money rails gem is one of these gems. At first glance, you may think it is needed for anytime you are dealing with money in your application. In many cases, you would be wrong. The functionality of the rails money gem is specifically for those pesky currency methods you wouldn’t wanna spend the time to write. If you just want whole dollars, you can use integers, they’ll work just fine. If you need to handle cents, SQL kids think DECIMAL(19, 4) format works best for databases but a decimal type with a precision of 8 and a scale of 2 will work just fine as well(depending on what you need). Both of these examples are referencing decimal as the data type you would use. The precision I referenced is the total digits of the number left of the decimal and the scale references how many to the right.

If you’ve gotten this far, it means you probably think you need it. The money-rails gem is just RubyMoney gem formatted for rails. You should follow the instructions laid out on https://github.com/RubyMoney/money-rails but some little tidbits I had picked up on were adding monetize type in your tables instead of integer. Whatever column name you have the monetize type for, you will also need to go into that model and add monetize :nameofcolumn_cents. Adding the cents will do that rails magic we all adore. It is very important that you realise this creates a money object. If you are working with any foreign currency, the default is USD but you can change that if need be in the initializers/money.rb you set up or override the default at the top of the ActiveRecord model.

Currency exchange is where this gem really starts shining. You can manually add currency exchange rates through an exchange bank object and that by itself can be pretty useful depending on what you are using it for. You also have the ability to create an exchange rate class to store conversions.

Example form the currency exchange section of the RubyMoney gem
Example form the currency exchange section of the RubyMoney gem

from here you can begin to add rates like so:

I know what you’re thinking, wouldn’t it be cool to use a scraper for this? YOU CAN! It even has some tips for that too.

Displaying the data in your project might be a bit of a headache at first but once you find all the different commands you can use, they come in handy real quick. Out of all the helpers that it provides, these two work best.

Passing in your money object into humanized_money_with_symbol( ) will display a nice looking number that reads just how we’d expect to see money displayed.

This only scratches the surface of the functionality of the Money Rails gem but this is a good starting block for those who decide they actually need this gem. Good luck and I hope you get to see those dollars rolling through your Rails application.

--

--