Forum: Ruby on Rails rails-y way to handle quasi-numeric type

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-03-18 20:59
(Received via mailing list)
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.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-03-19 03:03
(Received via mailing list)
Hello Larry,

2006/3/18, Larry White <ljw1001@gmail.com>:
> 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/Ag...

Hope that helps !
This topic is locked and can not be replied to.