Федор Усаков
Федор Усаков

Reputation: 1255

Ruby on Rails. Can't create a record in database

Having a problem while creating a new record in database.
Model currently looks like this:

class User < ActiveRecord::Base
      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i

      before_save {!self.email.downcase}

      validates :name, presence: true, length: {maximum: 50}
      validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}

      has_secure_password
      validates :password, presence: true, length: {minimum: 6}
end

Initially tried to create a record from the code, but failed without any errors. As a next step I tried to create a record manually in console, but still with the same result. No errors available. Skipping validations, reseting database, restarting server or excluding email REGEXP (always a source of troubles for me) from validations doesn't help...

user = User.new(name: "Fedor U", email: "[email protected]", password: "asdzxc123", password_confirmation: "asdzxc123")
=> #<User id: nil, name: "Fedor U", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$89SABxu2lOo3gj6DiqLZEOlSheAfEk0ex.5GYmJ5e8i...">

>> user.save
(0.2ms)  begin transaction
User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms)  rollback transaction
=> false
>> user.errors.any?
=> false
user.errors.full_messages
=> []

>> user.save(validation: false)
(0.1ms)  begin transaction
User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms)  rollback transaction
=> false

Any ideas what am I doing wrong? Rails 4.2.0, bcrypt 3.1.7.
Database is empty at the moment.

Upvotes: 0

Views: 213

Answers (1)

Sachin R
Sachin R

Reputation: 11876

!self.email.downcase will return true or false

use self.email.downcase! to change the email into smallcase.

Upvotes: 2

Related Questions