RubyMoney / money-rails

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

Conversion from price to price_cents using :with_currency is inaccurate for currencies without subunits #696

Open basabin54 opened 6 months ago

basabin54 commented 6 months ago

This is fairly niche and I have work arounds, but I figured I'd bring this up just in case there is an easy fix

Assuming the following models:

Class Product < ApplicationRecord
  # has attribute currency

  has_many :variants
end
Class Variant < ApplicationRecord
  belongs_to :product

  monetize :price_cents, with_currency: ->(variant) { variant.product.currency }
end

then when creating a variant through the relationship like this assuming a product with currency = "KRW" (South Korean Won - a currency without subunits):

@product.variants.new(price: 20)

I get back a variant with price_cents set to 2000 when it should only be 20 since KRW units and subunits should be equal.

Seems like :price_cents is being converted from :price with the default system currency when the currency through the relationship to product should be used