How (Replace|Create) an Enum Field on Rails 2.0 Migrations

How do I describe an enumeration column in a Rails 3 migration?

Rails 4.1 contains enum for now!

You can write just

class User < ActiveRecord::Base
enum status: [ :admin, :user, :banned ]
end

For migration write

t.integer :status

Rails 3 & 4.0

Best solution in my opinion is simple_enum gem.

Rails global enums?

It may be less likely that I need to
access it in my model, but definitely
within the controller and view.

Are you sure? When you talk about database and schema, you are talking about the Model part of your application.
I think the best place to store these variables would be the model that uses them.

If these variables belongs to a single model you can store them directly in the model itself:

class Item < ActiveRecord::Base
STATUS_UNKNOWN = 0
STATUS_MEDICAL = 3
end

Then you can reference the values inside and outside the model scope

class Item
def my_method
STATUS_UNKNOWN
end
end

Item::STATUS_UNKNOWN # => 0
Item.new.my_method # => 0

When there is an enumeration of values, Rubyists often use hashes or arrays.

class Item
AVAILABLE_STATUSES = { :unkwnown => 0, :medical => 3 }

def self.statuses
self.AVAILABLE_STATUSES.keys.sort
end

def self.status_value(key)
self.AVAILABLE_STATUSES[:key]
end

end

Item::AVAILABLE_STATUS # => { :unkwnown => 0, :medical => 3 }
Item.statuses # => [:medical, :unkwnown]
Item.status_value(:medical) # => 3

If more than one model share the same logic, you can put it in a Module and mix the module in all Models that requires it.

Proper storage of enumeration models in Rails

Check out the ruby gem I've been working on called classy_enum. I'm pretty sure it does exactly what you're looking for. The README has some example usage, but the premise is that it lets you define multiple enum members as classes that can have different properties.

Populating a Rails class in the migration

This is a combination of two answers already given, but this should work for you:

class CreateUserRoles < ActiveRecord::Migration
def self.up
create_table :user_roles do |t|
t.string :role
t.timestamps
end

UserRole.create :role => "System administrator"
UserRole.create :role => "Simulation controller"
end

def self.down
drop_table :user_roles
end
end

Rename your class UserType to UserRole (along with the associated test classes, assuming you created all of these using the generators). Rails uses the 'type' column for single table inheritance, and automatically populates the field with a class name when you have models that derive from a base class.

no implicit conversion of Symbol into Integer when migrating in production

Your migration syntax seems to be incorrect for the power_enum gem that you're using.

The gem's README says the syntax uses a name, then an options hash, such as:

create_enum("status", {…})

Whereas you're using a name, then an array:

create_enum("eh_released_inventory_status", […])

As far as I can tell, the solution is to change your migration line from what you posted to the power_enum syntax. The README has a bunch of examples, and also looks like it's thorough and very well documented.

How to prevent gmaps4rails from geocoding during migrations

I don't know how you proceed your migrations.

The idea to what you desire is to change the gmaps4rails_options instance method (source code here).

Either use a class_eval or an instance_eval to change it on the fly.



Related Topics



Leave a reply



Submit