On Jun 11, 8:36 pm, Dave Howell <[email protected]> wrote:
> I mentioned a "percentage" column in my database in a different thread. It's
> a customized numeric type that can only range from 0.0000 to 1.0000. (Yes,
> 'percentage' is a bit of a misnomer: it needs to be multiplied by 100 to
> actually be a percentage.) When I retrieve a value from this column, let's
> say it's 10%, instead of getting "0.1" I get
> #<BigDecimal:1017afd30,'0.1E0',4(12)>.
>
> This was what I did to get it to provide a float:
>
> class Component < Sequel::Model
> alias_method :bdpercentage, :percentage
> def percentage
> bdpercentage.to_f
> end
> end
>
> It seems to work, but I haven't been using Sequel long enough to know for
> sure what the ramifications might be. Will I need to also define a setter on
> percentage, or will a float automatically convert back to something Postgres
> would accept? (I might want to add a validator, of course, since Postgres
> will reject an attempt to set percentage to "1.3", for instance.)
In Sequel, you don't use aliasing to do that sort of thing. You use
super:
class Component < Sequel::Model
def percentage
super.to_f
end
end
You shouldn't need to define your own setter, as it should convert any
floats to big decimals.
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.