1

I have this class:

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email

  def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end
end

It is mapped to this table:

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_hash"
    t.string   "password_salt"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end

I have written this test:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "primary case" do
    my_user = User.new(users(:matching_password))
    assert my_user.save, "Didn't save valid user"
  end
end

My output is this:

  1) Error:
test_primary_case(UserTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column
 named password: INSERT INTO "users" ("email", "password", "password_confirmatio
n", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyString'
, '2013-04-23 02:39:43', '2013-04-23 02:39:43', 781772301)
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7-x86-mingw
32/lib/sqlite3/database.rb:91:in `initialize'
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7-x86-mingw
32/lib/sqlite3/database.rb:91:in `new'
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7-x86-mingw
32/lib/sqlite3/database.rb:91:in `prepare'
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7-x86-mingw
32/lib/sqlite3/database.rb:134:in `execute'
. . .

I have no problems when using this code live, so why the incorrect SQL now?

2
  • Mis-spelled or missing password column in the test database maybe? Apr 23, 2013 at 2:55
  • @JoachimIsaksson I have added the database table script to show there is no column for password. Instead, I added a property to hold this value in-memory
    – Camron B
    Apr 23, 2013 at 2:58

1 Answer 1

3

What is in your users.yml file? The users(:matching_password) call may be trying to save something in the password attribute.

2
  • It is. I modified the fixtures to set only the properties that would be set via mass assignment in an attempt to mirror user interaction as much as possible. Is this the correct approach?
    – Camron B
    Apr 23, 2013 at 14:04
  • The fixtures are not going through the Rails model, they write directly into the database (which is why they are fast). Any attribute you put there will end up in an SQL statement.
    – averell
    Apr 23, 2013 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.