How do I prevent Rails Console from performing an immediate rollback on db transaction?
-
26-05-2021 - |
Вопрос
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
Решение
So it looks like you weren't providing the password confirmation and that's why it wasn't saving.
Другие советы
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.