7

I am running "rails console" and then the following command:

 User.create(name:"John", email:"[email protected]", password:"foo", password_confirmation:"foo")

and i get this:

   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.1ms)  rollback transaction
 => #<User id: nil, name: "John", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$mY0/9RgjwOU46ZYcSC0TFOCMxrPiqWTEHWe1K27O/3Ya...">

when i check the file of the sqlite database using SQLite Database browser I see nothing.

here is my user model:

class User < ActiveRecord::Base

    #these attributes can be modified by the users
    attr_accessible :name, :email, :password, :password_confirmation
    #ruby's way of calling a method below...
    has_secure_password

    #validation testing
    validates :name, presence: true, length: { maximum: 50 }
    #regular expression (there is an official one)
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    #and add it..
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
        uniqueness:  { case_sensitive: false }
    #validate password 
    validates :password, length: {minimum: 6}
    validates :password_confirmation, presence: true

end

why is data not entered in my database?

I get this error with whatever I enter!

This for example:

1.9.3p125 :005 > User.create(name:"Smith", email:"[email protected]", password:"foo", password_confirmation:"foo")
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.0ms)  rollback transaction
 => #<User id: nil, name: "Smith", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$6nzyRJ0IplI6B4bSoQEtUOIcrbFVl1ix3EAKPGJZjZQf...">

I never entered a Smith user with that email, and I still get that "User Exists"!

EDIT:

I got the error. The password limit is 5 I was entering a 3-letter password so when I type this:

User.create(name:"Smith", email:"[email protected]", password:"foobar", password_confirmation:"foobar")
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (1.7ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00], ["email", "[email protected]"], ["name", "Smith"], ["password_digest", "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8rWPFfKusi"], ["updated_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00]]
   (266.9ms)  commit transaction
 => #<User id: 1, name: "Smith", email: "[email protected]", created_at: "2012-03-12 00:16:42", updated_at: "2012-03-12 00:16:42", password_digest: "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8...">

I works, but i still get this weird User Exists error... Any ideas?

4
  • 1
    It looks like you already have a user with the email [email protected] so the :uniqueness validation for the email is failing. Commented Mar 12, 2012 at 0:07
  • 1
    What output do you get if you do: user = User.new(name:"Smith", email:"[email protected]", password:"foo", password_confirmation:"foo") and then: puts user.errors? Commented Mar 12, 2012 at 0:15
  • 1
    @TestTest, try something gibberish: User.create(name:"fwjwof42848yt24", email:"[email protected]", password:"foo", password_confirmation:"foo") and see if you get the same problem. Commented Mar 12, 2012 at 0:15
  • 1
    always check user.errors - it'll tell you what's up. Commented Feb 20, 2013 at 20:47

8 Answers 8

8

Just got here using a Google Search and found what the issue is. In fact, it is a rubbish error message. Even with a clean database it would appear. The problem is: the password is only 3 characters long, wich will cause an issue with

    validates :password, length: {minimum: 6}

So, if you try with a longer password (and confirmation) it should work. (PS: I'm using MySQL server, not SQLite, but i'm pretty sure the error is the same)

Sign up to request clarification or add additional context in comments.

2 Comments

This was the solution for me. I was getting the error because my password was one character too short. Good catch Arthur!
I think "@messages={:password=>["is too short (minimum is 6 characters)"]}" is pretty clear really.
8
  1. user = User.new

  2. set the fields on user

  3. user.save

  4. look at user.errors

5 Comments

Checking user.errors is THE answer. There are so many different things that could go wrong.
Better yet, write an rspec test.
Unfortunately, that won't really help you understand what went wrong, just that something did. If you wrote a test, you'd want it to throw what was in user.errors anyway.
Sorry, that was implied. - there should be an expectation of the error. (ie. password verification failed, length error.)
Explicit is better than implicit...oh wait...this isn't Python. Thanks again for the great answer!
2

In fact, the problem is not "User Exists", and the real reason is that the password is too short. You can show the errors by inputing the user.errors on the rails console.

please refer to the following error example.

irb(main):013:0> user = User.new(name:"caiqinghua", email:"[email protected]", password:"admin",password_confirmation:"admin")

=> #<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">
irb(main):014:0> user.save
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.2ms)  rollback transaction
=> false
irb(main):015:0> User.all
  User Load (0.6ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
irb(main):016:0> User.create(name:"caiqing", email:"[email protected]", password:"admin",password_confirmation:"admin")
   (0.3ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.1ms)  rollback transaction
=> #<User id: nil, name: "caiqing", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$Ossfc7NsL6/MjYVEjT5rJe/y4AiqdNZI2tCkrN1h8rHx...">

**irb(main):017:0> user.errors**
=> #<ActiveModel::Errors:0xba7d34c0 @base=#<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">, @messages={:password=>["is too short (minimum is 6 characters)"]}>
irb(main):018:0> 

If i changed the password from "admin" to "admin123", and nothing is wrong.

irb(main):018:0> 
irb(main):019:0* user = User.new(name:"caiqinghua", email:"[email protected]", password:"admin123",password_confirmation:"admin123")
=> #<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">
irb(main):020:0> user.save
   (33.7ms)  begin transaction
  User Exists (9.9ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (108.8ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00], ["email", "[email protected]"], ["name", "caiqinghua"], ["password_digest", "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQb7wtj73Ha"], ["updated_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00]]
   (112.7ms)  commit transaction
=> true
irb(main):021:0> User.all
  User Load (0.6ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 2, name: "caiqinghua", email: "[email protected]", created_at: "2013-09-27 15:25:27", updated_at: "2013-09-27 15:25:27", password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">]>
irb(main):022:0> 

Comments

1

You are attempting to create a duplicate row in the database where a unique validation exists. See your error message ("User Exists"):

User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1

See the line in your model:

validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
    uniqueness:  { case_sensitive: false }

It's validating that the user's email address is unique. So you must already have a row in the database with "[email protected]" as the email address.

Comments

1

Maybe you are looking to the wrong database. This is rails app. and look to the balbla.development schema in database since there is no way about your problem that there must be a row that has same email value

Comments

1

I'm not 100% sure but I think

User Exists (0.4ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1

merely checks for uniqueness in that it will run regardless of whether your query will pass or fail.

If you pay a closer attention to the results, you will realize that a successful outcome will display 'User Exists' in blue, while a failed outcome when you actually try to insert a duplicate user will display 'User Exists' in red (or purple in my case).

Ryan

Comments

1

I encountered this problem too, because of my password is not longer than the 6. So I changed it to the longer one, it worked. But I didn't know why this exception named "User Existed", or
Something wrong with me. BYW, I didn't open the SQLIte Database Browser.

Comments

1

It's the password.

If you want to check the errors do this:

user = User.new(name:"John", email:"[email protected]", password:"foo", 
password_confirmation:"foo")

user.save

user.errors

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.