【发布时间】:2017-09-12 12:38:33
【问题描述】:
我试图寻找答案,但他们都在使用 User.new 等而不是创建。
当在控制台中使用具有有效属性的 User.create() 时,我得到一个用户,其 ID 为 nil,时间戳为 nil。
这是我的用户模型。
class User < ApplicationRecord
attr_writer :name, :email
before_save {self.email = email.downcase}
validates :username, presence:true,
length: {maximum: 30},
uniqueness:true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence:true,
length: {maximum:200},
format: {with: VALID_EMAIL_REGEX},
uniqueness: {case_sensitive: false}
has_secure_password
validates :password, presence: true,
length: {minimum:6}
end
在我正在使用的 rails 控制台中
User.create(username:"oiuedhioj", email:"damhan@dagr.com",password:"test",password_confirmation:"test")
然后回来
<User id: nil, username: "Damhan", email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$oGtRgcHigaHh/UCVX4QdM.AOgyGur8Oud5MyKZheUcQ...">
【问题讨论】:
标签: ruby-on-rails