Getting "ActiveRecord::UnknownAttributeError: unknown attribute: email_confirmation" Error with rspec

You're getting UnknownAttributeError because you don't have a column in your users table called email_confirmation. By default, ActiveRecord will look for DB columns named the same as the attributes you use to construct the model, but this line is trying to construct a User with an attribute the database doesn't know about:

  before { @user = User.new(email: "[email protected]",
                        first_name: "John", last_name: "Smith",
                        password: "foobar", password_confirmation: "foobar",
                        email_confirmation: "[email protected]") }

Are you really intending to save the email confirmation in the database, or are you just wanting to check that it matches email before saving it? I assume the latter, and Rails actually has built-in support for doing just that:

class User < ActiveRecord::Base
  validates :email, :confirmation => true
  validates :email_confirmation, :presence => true
end

See more details on the Rails Guide to Validations, or the validates_confirmation_of API docs. (And you'll probably need to do the same thing for :password_confirmation.)


Having just spent a ton of time debugging my own instance of this, I thought I would chime in with a third possibility.

I had done a migration correctly and verified it by inspecting my ActiveRecord in the rails console. I had tried recreating my db from the schema many times and I had tried re-running the migration many times, all to no avail.

The problem, in my case, is that I was seeing the problem when running my unit tests, not at runtime. The issue is that my test database had gotten out of sync in my migration/rollback testing. The solution was quite simple. All I had to do was reset the test database with:

rake db:test:prepare

I understand that the answer above is marked correct and solves the OP's problem. But there is another cause for this error that goes unheeded in a number of stackoverflow posts on this topic. This error can occur in a polymorphic many to many when you forget to use the as: option to a has_many. For example:

class AProfile < ActiveRecord::Base
  has_many :profile_students
  has_many :students, through: :profile_students
end

class BProfile < ActiveRecord::Base
  has_many :profile_students
  has_many :students, through: :profile_students
end

class ProfileStudent < ActiveRecord::Base
  belongs_to :profile, polymorphic: :true
  belongs_to :student
end

class Student < ActiveRecord::Base
  has_many :profile_students
  has_many :aprofiles, through: :profile_students
  has_many :bprofiles, through: :profile_students
end

This will give you this error:

Getting “ActiveRecord::UnknownAttributeError: unknown attribute: profile_id

when you try to do the following:

a = AProfile.new
a.students << Student.new

The solution is to add the :as option to AProfile and BProfile:

class AProfile < ActiveRecord::Base
  has_many :profile_students, as: :profile
  has_many :students, through: :profile_students
end

class BProfile < ActiveRecord::Base
  has_many :profile_students, as: :profile
  has_many :students, through: :profile_students
end