Skip to content

Instantly share code, notes, and snippets.

@prakashmurthy
Created December 13, 2010 03:16
Show Gist options
  • Save prakashmurthy/738608 to your computer and use it in GitHub Desktop.
Save prakashmurthy/738608 to your computer and use it in GitHub Desktop.
First code example in section 7.2.4 of Rails Tutorial
$ rails console --sandbox
>> User.create(:name => "Michael Hartl", :email => "mhartl@example.com",
?> :password => "foobar", :password_confirmation => "foobar")
>> user = User.find_by_email("mhartl@example.com")
>> user.has_password?("foobar")
=> true
$ rails console --sandbox
ruby-1.9.2-rc1 > User.create( :name => "Prakash Murthy", :email => "prakblr@yahoo.com", :password => "abcdefgh", :password_confirmation => "abcdefgh" )
=> #<User id: nil, name: "Prakash Murthy", email: "prakblr@yahoo.com", created_at: nil, updated_at: nil, encrypted_password: nil, salt: nil>
ruby-1.9.2-rc1 > user = User.find_by_email("prakblr@yahoo.com")
=> #<User id: 1, name: "Prakash Murthy", email: "prakblr@yahoo.com", created_at: "2010-12-12 01:42:44", updated_at: "2010-12-12 01:42:44", encrypted_password: nil, salt: nil>
ruby-1.9.2-rc1 > user.has_password?('abcdefgh')
=> false
================================================
Couldn't figure out why the encrypted_password and salt were nil; reviewed the previous steps in the chapter figure out this issue, assuming I must have missed some step.
Finally realized that the record User record being displayed was already in the database before I opened the rails console; I must have saved it in a previous exercise. Using create! instead of create throws the error immediately.
================================================
$ rails c --sandbox
Loading development environment in sandbox (Rails 3.0.0)
Any modifications you make will be rolled back on exit
ruby-1.9.2-rc1 > User.all
=> [#<User id: 1, name: "Prakash Murthy", email: "prakblr@yahoo.com", created_at: "2010-12-12 01:42:44", updated_at: "2010-12-12 01:42:44", encrypted_password: nil, salt: nil>]
ruby-1.9.2-rc1 > User.create!( :name => "Prakash Murthy", :email => "prakblr@yahoo.com", :password => "abcdefgh", :password_confirmation => "abcdefgh" )
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
@kavinfd
Copy link

kavinfd commented Jul 14, 2014

I had undergone the same problem..
just try with some other name and email address it worked for me.. since the very same record is already in the db changing the given data or removing the given record from the db will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment