6

my rspec test is giving me

 NameError:
   undefined local variable or method `confirmed_at' for #<User:0xca6ff98>

My User spec is:

require 'spec_helper'

describe User do

    before(:each) do
      @user = Factory(:user)
    end
    # Factory will make sure that in the future if attributes are added the tests below don't break
    # Just as long as the Factory is updated for the new attributes as appropriate.

    context "email is null" do
      it "record is invalid " do
        @user.name = nil
        @user.should_not be_valid
      end
    end

    context "email is unique" do
      it "record is valid " do
        @user2 = Factory(:user)
        @user2 = @user.email
        @user2.should_not be_valid 
      end
    end

end 

I can't find any reference to confirmed_at

I have devise 1.4.8 My user migration was:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

My user table is:

mysql> describe users;
+------------------------+--------------+------+-----+---------+----------------+
| Field                  | Type         | Null | Key | Default | Extra          |
+------------------------+--------------+------+-----+---------+----------------+
| id                     | int(11)      | NO   | PRI | NULL    | auto_increment |
| email                  | varchar(255) | NO   | UNI |         |                |
| encrypted_password     | varchar(128) | NO   |     |         |                |
| reset_password_token   | varchar(255) | YES  | UNI | NULL    |                |
| reset_password_sent_at | datetime     | YES  |     | NULL    |                |
| remember_created_at    | datetime     | YES  |     | NULL    |                |
| sign_in_count          | int(11)      | YES  |     | 0       |                |
| current_sign_in_at     | datetime     | YES  |     | NULL    |                |
| last_sign_in_at        | datetime     | YES  |     | NULL    |                |
| current_sign_in_ip     | varchar(255) | YES  |     | NULL    |                |
| last_sign_in_ip        | varchar(255) | YES  |     | NULL    |                |
| created_at             | datetime     | YES  |     | NULL    |                |
| updated_at             | datetime     | YES  |     | NULL    |                |
| last_name              | varchar(255) | YES  |     | NULL    |                |
| first_name             | varchar(255) | YES  |     | NULL    |                |
+------------------------+--------------+------+-----+---------+----------------+
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • 1
    Does Devise require a `confirmed_at` in your user? I've never used it so that's just a wild guess. – mu is too short Oct 22 '11 at 02:48
  • Thanks mu! Yeah it seems so but it seems weird that it's generator doesn't include it and before I go and add it manually I wanted to see if anyone else had seen this. – Michael Durrant Oct 22 '11 at 03:05
  • Missing migration perhaps? http://groups.google.com/group/plataformatec-devise/browse_thread/thread/f7d79b0cdada6992 – mu is too short Oct 22 '11 at 03:14
  • yeah that helped thanks. I had to add confirmed_at:datetime and confirmation_token:string and confirmation_sent_at:datetime in thend (Whew!). But I did add them one by one and then finally I could get specs to pass. I dunno this seems like maybe a temp hitch. Post your comment plus this as an answer and I can accept it. I will watch out for it. – Michael Durrant Oct 22 '11 at 05:00

2 Answers2

7

Looks like you missed the "confirmable" configuration for Devise somewhere along the way. You can add the three missing columns yourself or try something like this (see Reza.Hashemi's message near the bottom of this thread:

$ rails g migration addconfirmable

Then edit the migration to this:

def self.up 
  change_table(:users) do |t| 
    t.confirmable 
  end 
end 

and finally, the usual:

$ rake db:migrate

I think the above process should add your three columns:

  • confirmed_at :datetime
  • confirmation_token :string
  • confirmation_sent_at :datetime

for you. Or you can do it by hand.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

For Mongoid, look in the user model -- the needed fields may be commented out. For me, they were:

## Confirmable
field :confirmation_token,   :type => String
field :confirmed_at,         :type => Time
field :confirmation_sent_at, :type => Time
field :unconfirmed_email,    :type => String # Only if using reconfirmable
nachbar
  • 2,643
  • 1
  • 24
  • 34