Hi, I''m having problems with composed_of I have this two classes: class Declaration < ActiveRecord::Base composed_of :amount, :class_name => "Currency", :mapping => %w(amount amount) end class Currency attr_accessor :round, :decimal, :amount def initialize(amount) self.amount = amount self.round = amount.to_i.to_s[0..-3] self.decimal = amount.to_i.to_s[-2..-1] end end What I want to do: I want "amount" of the declaration model to be an object. I''m trying to execute the following;>> declaration = Declaration.new=> #<Declaration:0xb739f1b8 @attributes={"updated_at"=>nil, "description"=>nil, "amount"=>nil, "user_id"=>nil, "created_at"=>nil}, @new_record=true>>> declaration.amount = 12NoMethodError: undefined method `amount'' for 12:Fixnum from (eval):3:in `amount='' from (irb):2>>I would like to be able to do: declaration.amount = 1200 declaration.amount.cents() # or something like that) What am I doing wrong? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
LeonB wrote:> class Declaration < ActiveRecord::Base > composed_of :amount, :class_name => "Currency", :mapping => > %w(amount amount) > end > > class Currency > attr_accessor :round, :decimal, :amount > > def initialize(amount) > self.amount = amount > self.round = amount.to_i.to_s[0..-3] > self.decimal = amount.to_i.to_s[-2..-1] > end > > I would like to be able to do: > declaration.amount = 1200declaration.amount = Currency.new(1200) -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. But doing Currency.new() every time seems a bit like a hassle. On May 5, 12:51 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> LeonB wrote: > > class Declaration < ActiveRecord::Base > > composed_of :amount, :class_name => "Currency", :mapping => > > %w(amount amount) > > end > > > class Currency > > attr_accessor :round, :decimal, :amount > > > def initialize(amount) > > self.amount = amount > > self.round = amount.to_i.to_s[0..-3] > > self.decimal = amount.to_i.to_s[-2..-1] > > end > > > I would like to be able to do: > > declaration.amount = 1200 > > declaration.amount = Currency.new(1200) > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ah, added this tot the model: def amount=(amount) @amount = Currency.new(amount) end that worked nicely. On May 6, 3:38 pm, LeonB <l...-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote:> Thanks for the reply. But doing Currency.new() every time seems a bit > like a hassle. > > On May 5, 12:51 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > LeonB wrote: > > > class Declaration < ActiveRecord::Base > > > composed_of :amount, :class_name => "Currency", :mapping => > > > %w(amount amount) > > > end > > > > class Currency > > > attr_accessor :round, :decimal, :amount > > > > def initialize(amount) > > > self.amount = amount > > > self.round = amount.to_i.to_s[0..-3] > > > self.decimal = amount.to_i.to_s[-2..-1] > > > end > > > > I would like to be able to do: > > > declaration.amount = 1200 > > > declaration.amount = Currency.new(1200) > > > -- > > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---