Hi,
If I wanted to persist different Ruby types (e.g. float, BigDecimal,
integer, string) to a database based table (e.g. column names KEY and
VALUE), if I use the “to_yaml” method to serialize their values and
store
them as strings, what’s the easiest way to retrieve them back to a Ruby
variable such that they are of the original type (i.e. not a YAML type).
E.g. Want to be able to do things such as (assume I have a model called
Config):
ir = Config.find_by_key('interest_rate") # and the variable should be
of
type BigDecimal
ir = Config.find_by_key('age") # and the variable should be of type
Integer
etc
Hi,
If I wanted to persist different Ruby types (e.g. float, BigDecimal,
integer, string) to a database based table (e.g. column names KEY and
VALUE), if I use the “to_yaml” method to serialize their values and store
them as strings, what’s the easiest way to retrieve them back to a Ruby
variable such that they are of the original type (i.e. not a YAML type).
If you’re using Rails, there’s a helper built in. For example, if your
column named “value” is the object you want stored that way:
class Whatever < ActiveRecord::Base
serialize :value
…
end
I have no idea if this is safe for untrusted data – I suspect it’s not.
So, use this with your own databases, not with random web services, at
least without putting some thought into it.