Rails class padding during migration

I am trying to create a wrapper for a simple table that is just used as an enumeration. Therefore, I want to immediately populate the table with my 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 followed the Rails wiki and expected it to work.


Thanks. But what you suggested does not seem to work. Well, I do not see the lines.

 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 
+4
source share
5 answers

This is a combination of two answers already set, 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 UserType class to UserRole (along with the corresponding test classes, assuming you created them all with generators). Rails uses the "type" column to inherit from one table and automatically populates the field with the class name when you have models that are derived from the base class.

+3
source

try it

 class CreateUserTypes < ActiveRecord::Migration def self.up create_table :user_types do |t| t.string :role t.timestamps end UserType.create :role => "System administrator" UserType.create :role => "Simulation controller" end def self.down drop_table :user_types end end 

This is the self.up method that runs by default when rake db: migrate is called

Edit: the column name has changed to 'role' as 'type' is reserved for the inheritance of a single table. (See Comments). Apologize to Kyle Boone for receiving a very similar response.

+1
source

Disable the topic, but it is worth mentioning: it is considered a bad form for creating data in migrations, because the "official" way to create databases in the working environment is

 rake db:schema:load 

which, of course, will not load your data from the migration file. You can see one of my favorite plugins for this: Seed_Fu.

With Seed_fu, you create your data in YAML and then issue

 rake db:seed 

If you want to customize the data.

+1
source

HermanD's answer should work. I want to note that you have to be careful in using a column named "type" AFAIK, Rails uses this column name for STI.

0
source
  • Put creation instructions inside self.up
  • Call UserType.reset_column_information after creating the table and before filling it. The UserType model is before creating the table, so the rails collected information about the database structure until the table exists. You need to reset this so Rails will check the table attributes again.
  • Do not use 'type' as the attribute name. It was used for STIs.

    class CreateUserTypes <ActiveRecord :: Migration

     def self.up create_table :user_types do |t| t.column :role, :string t.timestamps end UserType.reset_column_information UserType.create :role => "System administrator" UserType.create :role => "Simulation controller" end def self.down drop_table :user_types end 

    end

0
source

All Articles