I’m trying to create a migration for a simple table that is just used as an enum. So I want to populate the table immediately with its values. I tried the following:
class CreateUserTypes < ActiveRecord::Migration def self.up create_table :user_types do |t| t.column :type, :string t.timestamps end end def self.down drop_table :user_types end UserType.create :type => 'System administrator' UserType.create :type => 'Simulation controller' end
but I get this error:
rake aborted! An error has occurred, all later migrations canceled: Could not find table 'user_types'
I was following the Rails wiki and expected it to work.
Thanks. But what you suggested doesn’t seem to be working. Well, I can’t see the strings.
sqlite> select * from user_types; 1||2009-02-08 12:00:56|2009-02-08 12:00:56 2||2009-02-08 12:00:57|2009-02-08 12:00:57
This is a combination of two answers already given, but this should work for you:
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.