Data not included in sqlite database - Ruby on Rails

I start the rails console and then the following command:

User.create(name:"John", email:" test@email.com ", password:"foo", password_confirmation:"foo") 

and I get the following:

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

when I check the sqlite database file 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 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+\-.] +@ [az\d\-.]+\.[az]+\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 data is not entered into my database?

I get this error with what I enter!

This is for example:

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

I have never entered Smith user with this email, and I still get this "User exists"!

EDIT:

I got an error. Password Limit: 5 I entered a 3 letter password so when I type this:

 User.create(name:"Smith", email:" smith@email.com ", password:"foobar", password_confirmation:"foobar") (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER(' smith@email.com ') 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", " smith@email.com "], ["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: " smith@email.com ", created_at: "2012-03-12 00:16:42", updated_at: "2012-03-12 00:16:42", password_digest: "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8..."> 

I work, but I still get this strange Exists user error ... Any ideas?

+7
source share
8 answers

Just here, using Google Search, and found out what the problem is. In fact, this is a garbage error message. Even with a clean database. The problem is that the password lasts only 3 characters, which will cause the problem with

  validates :password, length: {minimum: 6} 

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

+8
source
  • user = User.new

  • set fields to user

  • user.save

  • look at user.errors

+8
source

Actually, the problem is not with Custom Exists, and the real reason is that the password is too short. You can show errors by typing user.errors in the rails console.

refer to the following error example.

 irb(main):013:0> user = User.new(name:"caiqinghua", email:" caiqinghua@126.com ", password:"admin",password_confirmation:"admin") => #<User id: nil, name: "caiqinghua", email: " caiqinghua@126.com ", 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(' caiqinghua@126.com ') 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:" caiqing@126.com ", password:"admin",password_confirmation:"admin") (0.3ms) begin transaction User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(' caiqing@126.com ') LIMIT 1 (0.1ms) rollback transaction => #<User id: nil, name: "caiqing", email: " caiqing@126.com ", 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: " caiqinghua@126.com ", 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 happened.

 irb(main):018:0> irb(main):019:0* user = User.new(name:"caiqinghua", email:" caiqinghua@126.com ", password:"admin123",password_confirmation:"admin123") => #<User id: nil, name: "caiqinghua", email: " caiqinghua@126.com ", 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(' caiqinghua@126.com ') 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", " caiqinghua@126.com "], ["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: " caiqinghua@126.com ", 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> 
+2
source

You are trying to create a duplicate row in a database where a unique check exists. See Error Message ("User Exists"):

 User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER(' test@email.com ') LIMIT 1 

See the line in your model:

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

It confirms that the user's email address is unique. Thus, you should already have a row in the database with the name " test@email.com " as the email address.

+1
source

You may be looking for the wrong database. This is rails app. and look at the balbla.development schema in the database, since there is no problem with your problem, there should be a line with the same email value

+1
source

I'm not 100% sure, but I think

User exists (0.4ms) SELECT 1 FROM "users" WHERE LOWER ("users". "Email") = LOWER (' USer@example.com ') LIMIT 1

just checks for uniqueness in that it will run regardless of whether your request goes through or fails.

If you pay closer attention to the results, you will realize that a successful result displays “Custom Exists” in blue, while a failed result, when you actually try to insert a duplicate user, displays “Custom Exists” in red (or purple in my case).

Ryan

+1
source

I also ran into this problem because my password is no longer than 6. So, I changed it to a longer one, it worked. But I did not know why this exception is called "User Existed", or
Something is wrong with me. BYW, I did not open SQLIte Database Browser.

+1
source

This is the password.

If you want to check for errors, follow these steps:

 user = User.new(name:"John", email:" test@email.com ", password:"foo", password_confirmation:"foo") user.save user.errors 
+1
source

All Articles