Loading data in Rails migrations

I created a number of migrations starting with the definition for the Posts table.

class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.column "title", :string, :limit => 100, :default => "",  :null => false
      t.column "content", :text, :null => false
      t.column "author", :string, :limit => 100, :default => 0,  :null => false
      t.column "category", :string, :limit => 20, :default => "",  :null => false
      t.column "status", :string, :limit => 20, :default => "",  :null => false
      t.timestamps
    end
  end

  def self.down
    drop_table :posts
  end
end

      

And one more for the Users table in which I load some data for the default user after the table is created.

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column "username", :string,     :limit => 25, :default => "",   :null => false
      t.column "hashed_password", :string,  :limit => 40, :default => "", :null => false
      t.column "first_name", :string,   :limit => 25, :default => "",   :null => false
      t.column "last_name", :string,    :limit => 40, :default => "",   :null => false
      t.column "email", :string,        :limit => 50, :default => "",   :null => false
      t.column "display_name", :string, :limit => 25, :default => "",   :null => false
      t.column "user_level", :integer,  :limit => 3,  :default => 0,   :null => false
      t.timestamps
    end
    user = User.create(:username => 'bopeep',
      :hashed_password => 'bopeep',
      :first_name => 'Bo',
      :last_name => 'Peep',
      :email => 'bo@peep.com',
      :display_name => 'Little Bo Peep',
      :user_level => 9)
  end

  def self.down
    drop_table :users
  end
end

      

Next, I created a migration to change the Posts table to rename the table to blog_posts. Here I also wanted to load the default blog post.

class AlterPosts < ActiveRecord::Migration
  def self.up
    rename_table :posts, :blog_posts
    change_column :blog_posts, :author, :integer, :default => 0, :null => false
    rename_column :blog_posts, :author, :author_id
    add_index :blog_posts, :author_id
    bopeep = User.find_by_username 'bopeep'
    BlogPost.create(:title => 'test', :content => 'test', :author_id => bopeep.id, :category => 'test', :status => 'ok')
  end

  def self.down
    remove_index :blog_posts, :author_id
    rename_table :blog_posts, :posts
    rename_column :posts, :author_id, :author
    change_column :posts, :author, :string, :limit => 100, :default => 0, :null => false
  end
end

      

But this generates an error:

uninitialized constant AlterPosts::BlogPost

      

How do I load the default BlogPost instead of "BlogPost.create"?

+1


a source to share


1 answer


Separate your renamed_name and your changed / rename columns into a different migration file.



I don't think the rename is done until the whole block has gone through ... and therefore blog_posts doesn't exist yet.

+5


a source







All Articles