Created
December 13, 2010 03:16
-
-
Save prakashmurthy/738608 to your computer and use it in GitHub Desktop.
First code example in section 7.2.4 of Rails Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails console --sandbox | |
>> User.create(:name => "Michael Hartl", :email => "[email protected]", | |
?> :password => "foobar", :password_confirmation => "foobar") | |
>> user = User.find_by_email("[email protected]") | |
>> user.has_password?("foobar") | |
=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails console --sandbox | |
ruby-1.9.2-rc1 > User.create( :name => "Prakash Murthy", :email => "[email protected]", :password => "abcdefgh", :password_confirmation => "abcdefgh" ) | |
=> #<User id: nil, name: "Prakash Murthy", email: "[email protected]", created_at: nil, updated_at: nil, encrypted_password: nil, salt: nil> | |
ruby-1.9.2-rc1 > user = User.find_by_email("[email protected]") | |
=> #<User id: 1, name: "Prakash Murthy", email: "[email protected]", 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: "[email protected]", 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 => "[email protected]", :password => "abcdefgh", :password_confirmation => "abcdefgh" ) | |
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.