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

  • SEARCH
  • Home
  • 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 7080799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:50:23+00:00 2026-05-28T06:50:23+00:00

I had an add_column migration that would run fine. However, after running it and

  • 0

I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect–no errors reported. Here’s the original:

class UserAddFirstNameAndLastName < ActiveRecord::Migration
  def change
    # add column first name, last name string
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string

    User.all.each do |u|
      u.first_name = 'first name'
      u.last_name = 'last name'
      u.save
    end
  end
end

I also thought this might be some class loading issue, so I inserted the line User to force the user class to reload before the loop. No dice.

When I split this up into two migrations, the desired effect was achieved. Does someone have an explanation for this? I swear I’ve even done this in the same project with past migrations.

Other notes: Devise for user engine, added the new columns to attr_accessible in User class before running migration.

  • 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-28T06:50:23+00:00Added an answer on May 28, 2026 at 6:50 am

    You’re loading the Users class somewhere before your migration runs so User is a little confused about its own structure. The solution is to call reset_column_information after adding your column:

    Resets all the cached information about columns, which will cause them to be reloaded on the next request.

    The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values

    The Using Models in Your Migrations section of the Migrations Guide might be worth a look as well.

    Try rolling back and using a migration like this:

    def change
      # add column first name, last name string
      add_column :users, :first_name, :string
      add_column :users, :last_name, :string
    
      User.reset_column_information
    
      User.all.each do |u|
        u.first_name = 'first name'
        u.last_name = 'last name'
        u.save
      end
    end
    

    I checked this with three migrations like this:

    # 1: Don't touch Model before the new columns.
    def change
      add_column :models, :some_column, :string
      Model.all.each { |m| m.some_column = 'pancakes'; m.save }
    end
    
    # 2: Pull in Model before adding the new columns.
    def change
      puts Model.all.count
      add_column :models, :some_column, :string
      Model.all.each { |m| m.some_column = 'pancakes'; m.save }
    end
    
    # 3: Pull in Model before adding the new columns but use reset_column_information
    def change
      puts Model.all.count
      add_column :models, :some_column, :string
      Model.reset_column_information
      Model.all.each { |m| m.some_column = 'pancakes'; m.save }
    end
    

    The first one works just fine, the second one adds some_column but leaves it with NULL values, the third one also works.

    I’d guess that something in your application initialization (possibly from Devise) is causing User and its schema to be loaded, then you add a column. But, apparently, User only partly knows about the new column as the u.first_name call works but something is cached inside User to prevents the attribute from being written to the database.

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

Sidebar

Related Questions

Had a page that was working fine. Only change I made was to add
We had the Power Commands for Visual Studio 2008 that add a context menu
Had a good search here but can't see anything that gets my mind in
I had to add a new column to my table so I did that
I've got a query that's supposed to return 2 rows. However, it returns 48
When I was yound and stupid had little experiance, I decided it would be
I'm new to ActiveRecord. I realized that I had forgotten to add a default
OK i am really struggling with this! I know had to add an image
I had only one function to Add another row to the table id=tblOtherParties and
I had an idea, if I add a python .py file to my C#

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.