Question

A few classmates and I are having some trouble in our Rails app for class. Whenever we try to user the User.create for a RoR app on the rails console, we get a rollback transaction after we complete the necessary form.

Any way to fix this? Here is the error:

rails console
Loading development environment (Rails 3.2.13)
irb(main):001:0> User.count
   (0.0ms)  SELECT COUNT(*) FROM "users"
=> 0
irb(main):002:0> User.create(name: "Mr Rush", email: "rush@example.com", password: "fubar", password_confirmation: "fubar")
   (0.0ms)  begin transaction
  User Exists (0.0ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('rush@example.com') LIMIT 1
   (0.0ms)  rollback transaction
=> #<User id: nil, name: "Mr Rush", email: "rush@example.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$quwMA.4fcrBpg2sRy00qEOWrjHduAN0OZFvcXiCmNjUR...">
irb(main):003:0>

User.rb file

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = user.email.downcase }

  validates :name, presence: true, length: {maximum: 30}
  VALID_EMAIL_REGEX = /\A[\w+\-\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 5 }
  validates :password_confirmation, presence: true
end
Was it helpful?

Solution

Your user is failing to create because the email is failing the format validation.

It's interesting though that it reports this error as if the uniqueness is the thing causing the problem. You have shown your user table to be empty so no way it can fail a uniqueness constraint.

A couple of things to try:

1) in the console try this:

user = User.new(name: "Mr Rush", email: "rush@example.com", password: "fubar", password_confirmation: "fubar")
user.valid?     <--- should give you false
user.errors     <--- should tell you the format of the email is incorrect

When I combine presence with anything else in a validator I always allow the value to be blank in the following validations; there is no point in validating the format or uniquessness of something if it is mandatory but missing. i.e.

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

Doing this may or may not change what it thinks the real validation failure is.

2) fix the regex for email validation. Validating email address by regex is a bit controversial, but you should find something like this will work:

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

You can always try out the regex validation using a website like Rubular.

OTHER TIPS

User.create(name: "Mr Rush", email: "rush@example.com", password: "fubar", password_confirmation: "fubar")

When running into this while doing Michael Hartls Rails Tutorial this example will fail because the password fubar is too short, as set to minimum: 6 in Listing 6.39. The console is really less than verbose here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top