Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do enumerations in Rails 3?

In Rails 2 I know of a few plugins for enumerations such as acts_as_enumeration and enumerate_by but they don't seem to be maintained or updated for Rails 3. Preferably, the solution would store the enum in memory rather than a database for performance reasons but really any method would be useful since it can always be cached.

I did find enumerated_attribute that claims to work with Rails 3 but quite honestly I don't like the API and was hoping for another good solution.

(Sorry for only linking to the one plugin but it won't let me post more than one link until I get a higher reputation)

like image 457
Jon Dean Avatar asked Oct 30 '10 20:10

Jon Dean


People also ask

How do you declare an enumeration?

To declare an enumerationWrite a declaration that includes a code access level, the Enum keyword, and a valid name, as in the following examples, each of which declares a different Enum . Define the constants in the enumeration.

How do enumerations work?

Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

How do I rollback a migration in Rails?

To check for status, run rails db:migrate:status . Then you'll have a good view of the migrations you want to remove. Then, run rails db:rollback to revert the changes one by one. After doing so, you can check the status again to be fully confident.

What is enum and enumerations?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.


1 Answers

I am currently using lwe's simple_enum which seems to be actively developed and stores values on memory or if you prefer on a table.

like image 79
Jeduan Cornejo Avatar answered Sep 29 '22 22:09

Jeduan Cornejo