# Saving & Retrieving Ranges

**URL:** <https://www.ruby-forum.com/t/saving-retrieving-ranges/183524>\
**Category:** Rails\
**Created:** [February 16, 2010, 3:33am UTC](https://www.ruby-forum.com/t/saving-retrieving-ranges/183524 "2010-02-16T03:33:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 16, 2010, 3:33am UTC](https://www.ruby-forum.com/t/saving-retrieving-ranges/183524/1 "2010-02-16T03:33:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [February 16, 2010, 4:12am UTC](https://www.ruby-forum.com/t/saving-retrieving-ranges/183524/2 "2010-02-16T04:12:19Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
