这是最奇怪的事情:
> User.count
(74.7ms) SELECT COUNT(*) FROM "users"
=> 0
2.0.0p0 :002 > User
=> User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, name: string, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string)
2.0.0p0 :003 > User.create(email: "[email protected]", password: "test123", password_confirmation: "test123")
(140.0ms) BEGIN
User Exists (543.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
(0.3ms) ROLLBACK
=> #<User id: nil, email: "[email protected]", encrypted_password: "$2a$10$s7Ak0w04l3UixRqZbuvrJeLJb/AGD4FxQudSqcvYzDll...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, name: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil>
2.0.0p0 :004 > User.all
User Load (30.4ms) SELECT "users".* FROM "users"
=> []
> User.where(:email => "[email protected]")
User Load (24.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]'
=> []
发生了什么事?
Rails 3.2.13
编辑1
User.rb
与 Devise 和 Rolify 一样香草。
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
has_one :store
# has_many :items,
end
编辑2
我刚刚尝试过的另一件事,根据@whit-kimmey的建议:
> User.create!({email: "[email protected]", password: "test123", password_confirmation: "test123"}, without_protection: true)
(1.3ms) BEGIN
User Exists (105.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
(0.3ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 8 characters)
编辑3
所以,如果我将 pw 设为 8 个字符,它似乎可以工作 - 这很奇怪。但这仍然没有回答为什么它说具有该电子邮件地址的用户已经存在?
> User.create!({email: "[email protected]", password: "test-123", password_confirmation: "test-123"}, without_protection: true)
(0.2ms) BEGIN
User Exists (31.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
User Load (19.7ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'EZUGZPp8oEHMPSvExrgX' LIMIT 1
SQL (742.6ms) INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "name", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "unconfirmed_email", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id" [["confirmation_sent_at", Fri, 03 May 2013 17:47:43 UTC +00:00], ["confirmation_token", "EZUGZPp8oEHMPSvExrgX"], ["confirmed_at", nil], ["created_at", Fri, 03 May 2013 17:47:43 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "[email protected]"], ["encrypted_password", "$2a$10$0VoGOKvWXhfpOVrut6S6QeM1lkPgOYfq9s4Oo6zg.VMHZoJWTbc8G"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["name", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["unconfirmed_email", nil], ["updated_at", Fri, 03 May 2013 17:47:43 UTC +00:00]]
(116.5ms) COMMIT
=> #<User id: 1, email: "[email protected]", encrypted_password: "$2a$10$0VoGOKvWXhfpOVrut6S6QeM1lkPgOYfq9s4Oo6zg.VMH...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-05-03 17:47:43", updated_at: "2013-05-03 17:47:43", name: nil, confirmation_token: "EZUGZPp8oEHMPSvExrgX", confirmed_at: nil, confirmation_sent_at: "2013-05-03 17:47:43", unconfirmed_email: nil>
2.0.0p0 :008 >
Rails 只是执行该查询来查看该用户是否存在;这并不意味着它找到了一个。您在控制台输出中看到 Rails 正在执行的查询,但这个特定的查询没有找到以前存在的用户或导致任何问题。
试试这个:
User.create!({email: "[email protected]", password: "test123", password_confirmation: "test123"}, without_protection: true)
使用创建!而不是 create 将帮助我们查看 ActiveRecord 出现的错误。使用 without_protection 将绕过批量分配保护,以防万一出现问题。
您的结果表明您的其中一个 gem 对密码长度的验证失败。将密码更改为八个字符长即可解决问题。
它并没有说用户存在于任何地方,您只是查看 ActiveRecord 执行的查询来了解用户是否存在。如果您想获得实际的错误,请尝试以下操作:
> user = User.new(email: "[email protected]", password: "test123", password_confirmation: "test123")
> user.valid?
> user.errors.full_messages
检查验证。您可能正在验证名称或其他一些属性。
这可能是由于事务未提交,所以如果事务仍未提交,则使用以下命令来提交它:
ActiveRecord::Base.connection.commit_db_transaction