Problems with composed_of


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)

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]


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:0xb739f1b8 @attributes={“updated_at”=>nil,
“description”=>nil, “amount”=>nil, “user_id”=>nil, “created_at”=>nil},

declaration.amount = 12
NoMethodError: undefined method amount' for 12:Fixnum from (eval):3:inamount=’
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?

LeonB wrote:

self.round = amount.to_i.to_s[0..-3]
self.decimal = amount.to_i.to_s[-2..-1]


I would like to be able to do:
declaration.amount = 1200

declaration.amount =

We develop, watch us RoR, in numbers too big to ignore.

Thanks for the reply. But doing every time seems a bit
like a hassle.

Ah, added this tot the model:

def amount=(amount)
@amount =

that worked nicely.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs