Leveraging Rails' Money Type
You have a Rails application and need to persist some data through a form which has a JavaScript money mask.
Here in Brazil for instance, we format money this way: R$ 1.000,90.
Given this is a string and we need to save as decimal or integer, we have to convert.
The clean_money
above solves the problem, however, we always have to call it before set our data in the model (not that DRY).
Looking for a better way to do it I found two good solutions:
- Create your column as
money
type (Rails 4.2+) - Use the new attributes API (Rails 5+)
Both solutions only work if you are using PostgreSQL.
Column as money type
Attributes API
You just need it, really.
Then we can pass strings straight to our models:
See you.
Written on December 13, 2016