用户模型:
class User < ActiveRecord::Base
before_save { self.email == email.downcase }
attr_accessor :name, :email
validates :name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, length: { minimum: 6 }
end
控制台内部:
:001 > User.all
User Load (3.1ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
:002 > User.create(name: "Ryan Waits", email: "ryan.waits@gmail.com", password:
"foobar", password_confirmation: "foobar")
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =
LOWER('ryan.waits@gmail.com') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (6.8ms) INSERT INTO "users" ("created_at", "password_digest", "updated_at") VALUES
(?, ?, ?) [["created_at", Sat, 02 Nov 2013 16:20:10 UTC +00:00], ["password_digest",
"$2a$10$5MhI5u90tFqO3rrS58DW7eeNBdNCCveBA.IfqHp6OpwonYNaigyPO"], ["updated_at", Sat, 02
Nov 2013 16:20:10 UTC +00:00]]
(1.3ms) commit transaction
=> #<User id: 3, name: nil, email: nil, created_at: "2013-11-02 16:20:10", updated_at:
"2013-11-02 16:20:10", password_digest:
"$2a$10$5MhI5u90tFqO3rrS58DW7eeNBdNCCveBA.IfqHp6Opwo...">
(user = User.all) user.name 带回“用户”,电子邮件和密码属性带回用户对象上的未定义方法错误。
*笔记:
我创建了另一个具有不同属性的用户,结果相同,但名称、电子邮件、属性返回 nil 而不是“用户”和未定义的方法错误。
*更新:
所以我现在意识到,这条消息正在命中唯一性验证:
SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =
LOWER('ryan.waits@gmail.com') LIMIT
但是当我用新属性测试一个新用户时,使用 create 方法时它仍然返回 nil 属性。但是,像这样 (user.name = "adhsasd") 更新属性将创建一个用户。但是当我查询 User.all 时,属性又变为零,并且用户再次无效......我是否以某种方式访问了不同的数据库?我要转身了...