Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Money-Rails Gem - instance currencies

I have a model in my rails 4 app called funding.

I am using Money Rails to handle money/currency components - https://github.com/RubyMoney/money-rails

My funding model has 3 funding attributes called amount_expenses, amount_honorarium and amount_principal_financing.

The funding model also has a currency attribute for the user creating the instance to choose which currency should be used for each of the three funding attributes.

When I ran a migration to add_monetize to each of the three funding attributes, it created three corresponding currency attributes.

Do I need them? Can I ask the user to select a currency once per instance and then save the three funding attributes using that currency? How would that work? If I just have one currency attribute in the funding table, will monetise know how to choose that to display the three funding amounts?

The funding table has:

 t.boolean  "expenses"
    t.boolean  "honorarium"
    t.boolean  "financing"
    t.string   "currency"
    t.string   "size"
    t.integer  "amount_expenses"
    t.integer  "amount_honorarium"
    t.integer  "amount_principal_financing"
    t.float    "return_on_finance"
    t.integer  "period_of_return"
    t.text     "expense_description"
    t.integer  "scope_id"
    t.integer  "amount_expenses_pennies",             default: 0,     null: false
    t.string   "amount_expenses_currency",            default: "GBP", null: false
    t.integer  "amount_honorarium_pennies",           default: 0,     null: false
    t.string   "amount_honorarium_currency",          default: "GBP", null: false
    t.integer  "amount_principal_financing_pennies",  default: 0,     null: false
    t.string   "amount_principal_financing_currency", default: "GBP", null: false

end

Thank you

like image 963
Mel Avatar asked May 10 '15 01:05

Mel


1 Answers

For all of you three fields you can simply write

monetize :field1, :field2, :field3, with_model_currency: :currency_field

Only one currency column is sufficient in this case.

like image 132
Pardeep Dhingra Avatar answered Sep 30 '22 03:09

Pardeep Dhingra