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

Mel
  • 2,481
  • 26
  • 113
  • 273
  • 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. – davegson May 13 '15 at 09:07
  • I replied here how to use it, check if it helps you http://stackoverflow.com/questions/30010565/money-rails-gem-and-instance-currencies/30106138?noredirect=1#comment48341249_30106138 – Pardeep Dhingra May 13 '15 at 12:23
  • 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. – Mel May 15 '15 at 02:36

1 Answers1

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.

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
  • Hi, this didn't work for me, but I'm debugging other issues so it might be what's causing something to go wrong. – Mel May 20 '15 at 11:20
  • Thanks very much. I think I need to remove all the old currency refs and try again. I'll let you know. Thanks though – Mel May 20 '15 at 12:03
  • `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. – Jatto_abdul Apr 29 '20 at 16:53