Money-Rails Gem - instance currencies
Asked Answered
C

1

6

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

Cesium answered 10/5, 2015 at 1:38 Comment(3)
Could you document this a bit more? What currency attributes were created? Why should the user chose a currency for each attribute? Also, link the Gem you're using.Ammonate
I replied here how to use it, check if it helps you #30011065Jubilee
Hi, that's a different question. I have three attributes in one table that I want to monetise. The system has created 3 separate currency fields. I'd like to use one.Cesium
J
6

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.

Jubilee answered 13/5, 2015 at 12:27 Comment(3)
Hi, this didn't work for me, but I'm debugging other issues so it might be what's causing something to go wrong.Cesium
Thanks very much. I think I need to remove all the old currency refs and try again. I'll let you know. Thanks thoughCesium
with_model_currency option doesn't work for me with rails6 mongoid v7. field :price, type: Money, with_model_currency: :currency Getting the following error: Mongoid::Errors::InvalidFieldOption message: ` Invalid option :with_model_currency provided for field :price.` Any help would be appreciated.Gazpacho

© 2022 - 2024 — McMap. All rights reserved.