Leveraging Rails' Money Type
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.
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
moneytype (Rails 4.2+)
- Use the new attributes API (Rails 5+)
Both solutions only work if you are using PostgreSQL.
Column as money type
You just need it, really.
Then we can pass strings straight to our models: