Question

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.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top