我似乎无法在Employee的种子文件中创建一个对象。当我使用Rails控制台时,它不会让我:
irb(main):003:0> Employee.create(id: 1, user_name: "exampleemployee", passcode: "2345", password: "987654321")
(0.2ms) BEGIN
Employee Exists (0.4ms) SELECT 1 AS one FROM "employees" WHERE "employees"."user_name" = 'exampleemployee' LIMIT 1
Employee Exists (0.3ms) SELECT 1 AS one FROM "employees" WHERE "employees"."passcode" = '2345' LIMIT 1
(0.2ms) ROLLBACK
=> #<Employee id: 1, user_name: "exampleemployee", passcode: "2345", created_at: nil, updated_at: nil, password_digest: "$2a$10$7lpLZzAAJ5LsYLa/uKmfheyST3vfq7GY9hpoXVNECdL...">
irb(main):004:0> Employee.all
Employee Load (2.7ms) SELECT "employees".* FROM "employees"
=> #<ActiveRecord::Relation []>
irb(main):005:0>
当我从我的localhost:3000上的应用程序执行此操作时,它会创建Employee并且正常工作。
我已采取的故障排除步骤:重置数据库,重新播种文件并发生同样的事情。我正在使用Postgresql。我只使用bcrypt作为密码,这可能导致问题吗?
我有一个带有以下验证的Employee模型:
class Employee < ActiveRecord::Base
has_secure_password
validates_presence_of :user_name, :password, :password_confirmation, :passcode
validates_uniqueness_of :user_name, :passcode
validates :passcode, :numericality => {:only_integer => true}
validates_length_of :passcode, is: 4
validates_length_of :user_name, :password, minimum: 4
end
我的员工架构:
create_table "employees", force: true do |t|
t.string "user_name"
t.string "passcode"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
end
答案 0 :(得分:2)
您应该在create:
上设置next>next>install
而不是password
password_digest
更多信息:http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html