Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 207187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:42:48+00:00 2026-05-11T17:42:48+00:00

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

  • 0

I created number of migrations starting with a definition for a 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 another one for a Users table in which I load some data for a default user after creating the table.

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 alter the Posts table to rename the table to blog_posts. Here I also wanted to load a default blog post entry.

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 should I have loaded the default BlogPost instead of “BlogPost.create”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:42:48+00:00Added an answer on May 11, 2026 at 5:42 pm

    Separate your rename_table and your change/rename column migrations into another migration file.

    I don’t think the rename is committed until after the entire block goes through… and so therefore blog_posts does not exist yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 239k
  • Answers 239k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RAMDisk with freeware version Microsoft version How-to May 13, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer Take a look at the documentation for android.database.sqlite.SQLiteDatabase. In particular,… May 13, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer Will your table always only have two rows? Such as:… May 13, 2026 at 7:00 am

Related Questions

When project grows up, the number of migrations starts to be pretty high, and
I have a database set up for my Rails installation and some migrations set
I added a counter cache but can't get it to update. But I can
How do I take a range of commits in my SVN repository and combine

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.