Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data not entering sqlite database - Ruby on Rails

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?

like image 432
Test Test Avatar asked Mar 12 '12 00:03

Test Test


People also ask

Is SQLite compatible with Ruby on rails?

SQLite is supported by Ruby on Rails by default as a highly compatible database. It’s known as an internal database, used mainly to cover the needs of production and testing. It’s a common one for MVPs, local projects, and internal builds. SQLite is often used to set up the basic data structure and then replaced with a more powerful alternative.

What is the default database for Ruby on rails?

Ruby on Rails uses SQLite3 as its default database. While Sqlite works great with Rails, some times it may not be sufficient for your Rails application. If you want scalability, concurrency, centralization, and control, you may want to try more robust databases like MySQL or PostgreSQL.

Do I need to create a new database for SQLite3?

If you get warning messages that database files already exist do not worry since this is just an information that application has found newly created database. Namely for SQlite3 there is no need to create database in advance. Rails will do that for you if database does not exist.

How to connect Ruby on Rails to Postgres or MySQL?

If you established the Ruby connect to postgres database, you should see the Rails welcome message. If you are using ruby on rails, connect it to mysql database with a similar method. You need to prepare installed MySQL, open the root password from MySQL, and run Ruby on Rails.


1 Answers

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)

like image 173
Arthur Camara Avatar answered Nov 20 '22 05:11

Arthur Camara