ruby - Rails: money gem converts all amounts to zero -


i'm trying use money gem handle currency in app i'm running strange error. have in "record" model:

composed_of :amount,             :class_name => "money",             :mapping => [%w(cents cents), %w(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} money") } 

amount integer.

when create new record ignores whatever value put in amount field , defaults 0. there need add forms?

i'm using rails 3.0.3 , money gem version 3.5.5

edit: added bonus @ end of answer

well, question interesting me decided try myself.

this works properly:

1) product migration:

create_table :products |t|   t.string :name   t.integer :cents, :default => 0   t.string :currency   t.timestamps end 

2) product model

class product < activerecord::base     attr_accessible :name, :cents, :currency    composed_of :price,     :class_name => "money",     :mapping => [%w(cents cents), %w(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} money") } end 

3) form:

<%= form_for(@product) |f| %>   <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>    </div>   <div class="field">     <%= f.label :cents %><br />     <%= f.text_field :cents %>   </div>   <div class="field">     <%= f.label :currency %><br />          <%= f.select(:currency,all_currencies(money::currency::table), {:include_blank => 'select currency'}) %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

4) products helper (handmade):

module productshelper   def major_currencies(hash)     hash.inject([]) |array, (id, attributes)|       priority = attributes[:priority]       if priority && priority < 10         array ||= []         array << [attributes[:name], attributes[:iso_code]]       end       array     end   end    def all_currencies(hash)     hash.inject([]) |array, (id, attributes)|       array ||= []       array << [attributes[:name], attributes[:iso_code]]       array     end   end end 

bonus:

if want add currency exchange rates:

1) gemfile

gem 'json' #important, not set dependency, add manually gem 'google_currency' 

2) initializer

create money.rb in initializers folder , put inside:

require 'money' require 'money/bank/google_currency' money.default_bank = money::bank::googlecurrency.new 

reboot server

3) play!

wherever are, can exchange money.

product.first.price.exchange_to('usd') 

display nice rendering:

product.first.price.format(:symbol => true) 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -