Saving & Retrieving Ranges

I would like to be able to save a set of age brackets as a range and
have it
treated as a Ruby range when retrieved from the database. I’ve done
quite a
bit of searching (maybe my Google-fu just sucks) but haven’t had any
luck in
getting to an answer. Anyone have any thoughts on doing this or should
I
simply save the minimum / maximum and roll my own range-like methods?

On Feb 15, 2010, at 9:31 PM, Chris D. wrote:

I would like to be able to save a set of age brackets as a range and
have it treated as a Ruby range when retrieved from the database.
I’ve done quite a bit of searching (maybe my Google-fu just sucks)
but haven’t had any luck in getting to an answer. Anyone have any
thoughts on doing this or should I simply save the minimum / maximum
and roll my own range-like methods?

You could use serialize

class Person
serialize :age_range
end

teenager = Person.create(:age_range => 13…19)

But be aware that the actual field in the table will be a YAML-ized
representation as a string.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]