# Storing a value of an enum in the ActiveRecord model

**URL:** <https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977>\
**Category:** Rails\
**Created:** [August 18, 2009, 10:25pm UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977 "2009-08-18T22:25:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 18, 2009, 10:25pm UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977/1 "2009-08-18T22:25:22Z")

</div>

Hi, everyone!

What is a good way to store a enum value in the ActiveRecord?  
For example, I have a User model. The User has a “gender” that can only  
be :not\_specified, :male, or :female.

Gleb

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 18, 2009, 11:03pm UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977/2 "2009-08-18T23:03:36Z")

</div>

Gleb M. wrote:

> Hi, everyone!
> 
> What is a good way to store a enum value in the ActiveRecord?

You might want to use composed\_of or maybe even an association.

> For example, I have a User model. The User has a “gender” that can only  
> be :not\_specified, :male, or :female.

Do you know yet what the DB side of this will look like?

> Gleb

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 18, 2009, 11:10pm UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977/3 "2009-08-18T23:10:31Z")

</div>

Hi, Marnen!

> > What is a good way to store a enum value in the ActiveRecord?
> 
> You might want to use composed\_of or maybe even an association.

Thanks, I’ll look into it.

> > For example, I have a User model. The User has a “gender” that can only  
> > be :not\_specified, :male, or :female.
> 
> Do you know yet what the DB side of this will look like?

No, but I will appreciate an advice on the DB side of the things.

Cheers,  
Gleb

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 19, 2009, 2:47am UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977/4 "2009-08-19T02:47:15Z")

</div>

For now I settled with this gem

> **[GitHub - jeffp/enumerated\_attribute: Easy enum for your models, objects and...](https://github.com/jeffp/enumerated_attribute/tree/master)**
>
> master

But, frankly, I was surprised how difficult it is to do Java-like enums  
(enums that can have instance methods).

Still, since for now I only need enums for gender and marital status,  
this will do.

Thanks!

Cheers,  
Gleb

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 19, 2009, 2:59am UTC](https://www.ruby-forum.com/t/storing-a-value-of-an-enum-in-the-activerecord-model/173977/5 "2009-08-19T02:59:51Z")

</div>

Gleb M. wrote:

> For now I settled with this gem  
> [GitHub - jeffp/enumerated\_attribute: Easy enum for your models, objects and views](http://github.com/jeffp/enumerated_attribute/tree/master)

I’ll check that out. I hadn’t heard of it.

> But, frankly, I was surprised how difficult it is to do Java-like enums  
> (enums that can have instance methods).

What part is difficult about that? True, Ruby doesn’t have a native  
Enum class that I know of, but I would think it would be easy to make  
one.

> Still, since for now I only need enums for gender and marital status,  
> this will do.

OK.

> Thanks!
> 
> Cheers,  
> Gleb

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
