I have model Product with fields price_cents and price_currency. Money default currency is USD.
Model:
class Product < ActiveRecord::Base
CURRENCIES = %w(USD EUR)
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_cents cents), %w(price_currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
end
Form:
= form_for @product do |f|
= f.label :price
= f.text_field :price
= f.select :price_currency, Product::CURRENCIES
= f.submit
Controller Create method: @product = Product.new(params[:product])
Problem: When user sets price field to 100 for example and price_currency to EUR it creates price using default currency(USD). How I can fix it? Is it possible to do it in view or i should do it in controller (e.g. @product.price.currency = ...)?