# Problems with composed\_of

**URL:** <https://www.ruby-forum.com/t/problems-with-composed-of/103403>\
**Category:** Rails\
**Created:** [May 5, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/problems-with-composed-of/103403 "2007-05-05T12:10:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 5, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/problems-with-composed-of/103403/1 "2007-05-05T12:10:36Z")

</div>

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 = 12  
> > NoMethodError: 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?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 5, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/problems-with-composed-of/103403/2 "2007-05-05T12:51:38Z")

</div>

LeonB wrote:

> ```
> 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.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 6, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/problems-with-composed-of/103403/3 "2007-05-06T15:38:39Z")

</div>

Thanks for the reply. But doing Currency.new() every time seems a bit  
like a hassle.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 6, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/problems-with-composed-of/103403/4 "2007-05-06T22:05:18Z")

</div>

Ah, added this tot the model:

def amount=(amount)  
@amount = Currency.new(amount)  
end

that worked nicely.
