Hi, I''m getting this error:>> p = Plan.new >> p.amountTypeError: wrong argument type String (expected Fixnum) Apparently this happens because the aggregation isn''t initialized. Here''s the code: class Plan < ActiveRecord::Base composed_of :amount, :class_name => "Money", :converter => Proc.new { |s| Money.new(s) }, :mapping => [:price, :value] end class Money attr_reader :value def initialize(string) @value = BigDecimal(string.sub(/,/, ''.'')) end def to_s Integer(@value * 100).to_s.sub(/(\d{2})$/, '',\1'') end end I would like to hide the aggregation from controllers... There''s the after_initialization callback where I could (try to) initialize the value object if it is nil... I wonder what''s the proper way to deal with this situation? Thanks, -- Adriano --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adriano Nagel
2009-Jan-17 01:17 UTC
Re: Aggregation: problem with uninitialized composed_of object
For the record... On Jan 16, 4:17 pm, Adriano <adri...-S20CnuFijpQ39yzSjRtAkw@public.gmane.org> wrote:> TypeError: wrong argument type String (expected Fixnum)[snip]> def initialize(string) > @value = BigDecimal(string.sub(/,/, ''.'')) > endThe problem was that value object can be initialized with a string (from the form) or with a number (from the db). Thus initialize should be changed, eg: def initialize(value) @value = BigDecimal(value.to_s(/,/, ''.'')) end -- Adriano --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---