4

I'm new to rails and I'm having some trouble in the console. I'd like to add records to my Users table and test some functions. However, everytime i perform a User.create or similar function, it completes successfully and then immediately gets rolled back. How do I prevent the immediate rollback?

I am not in sandbox mode.

Below is the output I get in the console when trying to create a user. It says the user exists, then immediately rolls back the transaction. I then run a User.all just to show that the transaction did indeed get rolled back.

>>> User.create(first_name: "derek", last_name: "harrington", email: "derek@gmail.com")
 (0.1ms)  begin transaction
 User Exists (0.2ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'derek@gmail.com' LIMIT 1
 (0.1ms)  rollback transaction
 => #<User id: nil, first_name: "derek", last_name: "harrington", email: "derek@gmail.com", password_digest: nil, credit_card_id: nil, address_id: nil, created_at: nil, updated_at: nil> 
>>> User.all
 User Load (0.3ms)  SELECT "users".* FROM "users" 
 => [] 

How do I make these changes permanent and prevent the rollback?

Edit:

Here is the contents of my User model

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

  validates :first_name, presence: true, length: { maximum: 50 }
  validates :last_name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: true
  validates :password, confirmation: true, length: { minimum: 6, maximum: 50 }
  validates :password_confirmation, presence: true
end
Derek Harrington
  • 457
  • 4
  • 13
  • 1
    Post the contents of your `User` model. You should also assign the result of `User.create` to a variable so you can check the errors on it. – James Mar 26 '12 at 01:32
  • I assigned the results to a variable as you suggested and it seems to work as expected in the variable. It just won't stay in the database. – Derek Harrington Mar 26 '12 at 01:48
  • 4
    That's just an unsaved `User` instance, the point is to check the errors on it. If you have it in the variable `user` then check what you get from `user.errors`. – James Mar 26 '12 at 01:49
  • You need to surround your text with backticks to use inline code. – James Mar 26 '12 at 02:00
  • Got it. Figured it out. In my model, it specifies `password_confirmation` as required, and I wasn't including it when I was trying to create a `User`. The `user.errors` revealed that was the problem. Thanks so much for your help. – Derek Harrington Mar 26 '12 at 02:03

2 Answers2

4

So it looks like you weren't providing the password confirmation and that's why it wasn't saving.

James
  • 4,797
  • 25
  • 29
0

That's odd.

Did you add some configuration option? If so, better review them.

You can also try very simple rails app to verify the behavior.

shigeya
  • 4,862
  • 3
  • 32
  • 33