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
Ruby on Rails - Drop Down Box on Change Event
How to Find the Namespace/Module Name Programmatically in Ruby on Rails
Colon (:) Appears as Forward Slash (/) When Creating File Name
How to Require Active Record Working Outside of Rails
Rails Route to Username Instead of Id
Is There an Expect Equivalent Gem for Ruby
Prevent Rails Test from Deleting Seed Data
Installing Jekyll on Ubuntu 14.04
Is It Possible for Rspec to Expect Change in Two Tables
How to Prevent Nokogiri from Adding <Doctype> Tags
How to Generate Zip File Without Saving to the Disk with Ruby
Looping Through Bits in an Integer, Ruby
Add Http(S) to Url If It's Not There
What Does the * (Star) Mean in Ruby
Testing Error Pages in Rails with Rspec + Capybara
How to Get Sinatra to Refrain from Adding the X-Frame-Options Header