I have a Product model with the price_cents and price_currency fields. The currency of the 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
the form:
= form_for @product do |f| = f.label :price = f.text_field :price = f.select :price_currency, Product::CURRENCIES = f.submit
Controller creation method: @product = Product.new (params [: product])
Problem. When the user sets the price field to 100, for example, and price_currency in EUR, he creates a price using the default currency (USD). How can i fix this? Is it possible to do this, or should I do it in the controller (e.g. @ product.price.currency = ...)?
source share