Storing a value of an enum in the ActiveRecord model

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

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
[email protected]

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

For now I settled with this gem

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

Gleb M. wrote:

For now I settled with this gem
GitHub - jeffp/enumerated_attribute: Easy enum for your models, objects and views

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
[email protected]