Gleb Mazovetskiy
2009-Aug-18 20:25 UTC
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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-18 21:03 UTC
Re: Storing a value of an enum in the ActiveRecord model
Gleb Mazovetskiy 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?> > GlebBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Gleb Mazovetskiy
2009-Aug-18 21:10 UTC
Re: Storing a value of an enum in the ActiveRecord model
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 -- Posted via http://www.ruby-forum.com/.
Gleb Mazovetskiy
2009-Aug-19 00:47 UTC
Re: Storing a value of an enum in the ActiveRecord model
For now I settled with this gem http://github.com/jeffp/enumerated_attribute/tree/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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-19 00:59 UTC
Re: Storing a value of an enum in the ActiveRecord model
Gleb Mazovetskiy wrote:> For now I settled with this gem > http://github.com/jeffp/enumerated_attribute/tree/masterI''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, > GlebBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.