RubyMoney / money-rails

Integration of RubyMoney - Money with Rails
MIT License
1.8k stars 387 forks source link

Using with_model_currency While Referencing Currency on Associated Record #680

Closed chase-stevens closed 1 year ago

chase-stevens commented 1 year ago

Hi all 👋

I'm looking at a use case where I define a currency on one record, and then I would want to reference that currency on that record in associated records.

This works using with_model_currency: :currency and delegating the :currency method to the associated model, but wanted to see if there was a more elegant way to do this.

class Product < ApplicationRecord
  monetize :price_cents, with_model_currency: :currency

  belongs_to :shop

  delegate :currency, to: :shop
end
sunny commented 1 year ago

I guess there could be some kind of with_delegated_currency: :shop, but I find the code you have right now already quite short and elegant.

chase-stevens commented 1 year ago

@sunny The more I think about it (and hear feedback from the team), the more I am inclined to agree 👍