2

这是最奇怪的事情:

> 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: "abc@test.com", password: "test123", password_confirmation: "test123")
   (140.0ms)  BEGIN
  User Exists (543.7ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'abc@test.com' LIMIT 1
   (0.3ms)  ROLLBACK
 => #<User id: nil, email: "abc@test.com", 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 => "abc@test.com")
  User Load (24.8ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'abc@test.com'
 => [] 

怎么了?

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: "abc@test.com", password: "test123", password_confirmation: "test123"}, without_protection: true)
   (1.3ms)  BEGIN
  User Exists (105.5ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'abc@test.com' LIMIT 1
   (0.3ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 8 characters)

编辑 3

所以,如果我制作 pw 8 个字符,它似乎可以工作 - 这很奇怪。但这仍然不能回答为什么它说具有该电子邮件地址的用户已经存在?

> User.create!({email: "abc@test.com", 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" = 'abc@test.com' 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", "abc@test.com"], ["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: "abc@test.com", 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 > 
4

3 回答 3

4

Rails 只是在执行该查询以查看该用户是否存在;这并不意味着它找到了一个。您在控制台输出中看到 Rails 正在执行的查询,但是这个特定的查询没有找到以前存在的用户或导致任何问题。

试试这个:

User.create!({email: "abc@test.com", password: "test123", password_confirmation: "test123"}, without_protection: true)

使用创建!而不是 create 将帮助我们看到 ActiveRecord 得到的错误。使用 without_protection 只会绕过批量分配保护,以防万一出现问题。

您的结果表明您的其中一个 gem 的密码长度验证失败。将密码更改为八个字符长将解决问题。

于 2013-05-03T17:32:54.050 回答
2

它并不是说用户存在于任何地方,您只是查看 ActiveRecord 执行的查询以了解用户是否存在。如果你想得到实际的错误试试这个:

> user = User.new(email: "abc@test.com", password: "test123", password_confirmation: "test123")
> user.valid?
> user.errors.full_messages
于 2013-05-03T17:52:02.123 回答
0

检查验证。您可能正在验证名称或其他一些属性。

于 2013-05-03T17:19:40.337 回答