# Rails-y way to handle quasi-numeric type

**URL:** <https://www.ruby-forum.com/t/rails-y-way-to-handle-quasi-numeric-type/55269>\
**Category:** Rails\
**Created:** [March 18, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/rails-y-way-to-handle-quasi-numeric-type/55269 "2006-03-18T20:59:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 18, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/rails-y-way-to-handle-quasi-numeric-type/55269/1 "2006-03-18T20:59:47Z")

</div>

I have a class called Duration that represents a unit of time. It has  
lots  
of time-related methods and an attribute that represents the duration in  
seconds.

I want to store it in my db. I had a column type of NUMERIC, so if i  
save  
it, rails trys to coerce it to a float automagically and fails.

I need to display it and store it as a number but treat it in the middle  
layer as a Duration so I can use all it’s wonderful functionality.  
What’s  
the right way to do this?

thanks.

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 19, 2006, 3:03am UTC](https://www.ruby-forum.com/t/rails-y-way-to-handle-quasi-numeric-type/55269/2 "2006-03-19T03:03:10Z")

</div>

Hello Larry,

2006/3/18, Larry W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I need to display it and store it as a number but treat it in the middle  
> layer as a Duration so I can use all it’s wonderful functionality. What’s  
> the right way to do this?

You want to use composed\_of

class UserOfDuration \< ActiveRecord::Base  
composed\_of :project\_length, :class\_name =\> ‘Duration’, :mappings =\>  
%w(duration\_in\_seconds duration)  
end

[http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html#M000692](http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html#M000692)

Hope that helps !
