Ask A Question

Notifications

You’re not receiving notifications from this thread.

Handle money in Rails

Wouter van den Beld asked in Rails

Hi,

What do you think is the best way to handle money in rails?
At the moment i use a decimal field with a precision if 16 and a scale of 2.

the problem is that we use the comma as a decimal sign and this sometimes becomes a problem.

should i use the money gem or use only cents or do you guys have a better way to handle this?

Reply

Oh that's a really interesting question. I've never really considered that because we only use it in the other notation.

I would imagine that the Money gem can do the conversion for you and that internally it really saves them as decimals but tweaks the string output so that it uses comma for the decimal. I think you would want to continue saving with the decimal format and then have a method that converts the value into a Money object and that would be what you interact with. You could also probably have a setter method that lets you set the value via the comma version of the string which converts to the decimal one in the database.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.