0 2.0.0p0 :002 > User => User(-6ren">
gpt4 book ai didi

ruby-on-rails - 为什么 Rails 控制台告诉我没有记录存在,但我无法创建记录,因为它存在?

转载 作者:行者123 更新时间:2023-12-02 17:44:48 25 4
gpt4 key购买 nike

这是最奇怪的事情:

> 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 >

最佳答案

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

试试这个:

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

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

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

关于ruby-on-rails - 为什么 Rails 控制台告诉我没有记录存在,但我无法创建记录,因为它存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com