NoMethodError: undefined method `password_digest=

I kept coming across this error when I ran the rails console and tried to set a user password:

NoMethodError: undefined method `password_digest=

It wasn’t written in the guide, but to solve this problem, you have to run a migration and create a new database which I called AddPasswordDigestToUsers (following Hartl’s code on Github).

$ rails generate migration AddPasswordDigestToUsers password_digest: string

Then a new file will be created with the following code written in it:

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end

Michael Hartl’s code on Github

Now, you should be able to run the $rails console and add a password to your user.

$rails console
>> User.first
>>user = User.first
>>user.password = “fresh”

Advertisements

Chapter 7 | Hartl | ActiveRecord::PendingMigrationError

Error! I’m on Chapter 7 of Michael Hartl’s tutorial and came across this error when I ran the rails server to view my app:

~workspace/sample_app (signup) $ bundle exec rake db:migrate RAILS_ENV=development
== 20150709195838 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "email" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /usr/local/rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `initialize'

This is the error that showed up on the main page of the web application:

raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)

Having no idea what this meant, I naturally did a search and found this answer on Stack Overflow that solved my problem.

I received a similar error when running rake:db migrate. To resolve my issue I ran rake:db drop to drop my database since I was in dev mode with no production database. Then I recreate the database with rake db:create after which i ran rake db:migrate successfully.

Error running rake db:migrate ActiveRecord::PendingMigrationError Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.

Resolved using:

rake db:drop - this will wipe the data out of your database rake db:create rake db:migrate

So I followed the instructions and ran ‘rake db:drop’ to clear my database. Then ran ‘rake db:create’ followed by ‘rake db:migrate’ and ‘finally $bundle exec rake db:migrate RAILS_ENV=production’ and my web app worked!

Unnamed QQ Screenshot20150721005531