Ruby 验证是否存在密码,而不在Sequel中包含密码列
我正在尝试验证是否存在“密码”,尽管我不希望数据库中有密码列。。。我把密码转换成密码。在没有密码列的情况下,如何验证保存前加密是否存在密码 模式:Ruby 验证是否存在密码,而不在Sequel中包含密码列,ruby,sequel,Ruby,Sequel,我正在尝试验证是否存在“密码”,尽管我不希望数据库中有密码列。。。我把密码转换成密码。在没有密码列的情况下,如何验证保存前加密是否存在密码 模式: migration "create users table" do database.create_table :users do primary_key :id text :name text :email text :username text :
migration "create users table" do
database.create_table :users do
primary_key :id
text :name
text :email
text :username
text :password_hash
text :password_salt
index :username, :unique => true
end
end
用户模型:
class User < Sequel::Model
require 'bcrypt'
def before_save
if password
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def validate
super
validates_presence [:password,:email] if new?
validates_unique [:email]
end
def self.authenticate(email, password)
user = User.find(:email=>email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
class用户email)
如果user&&user.password\u hash==BCrypt::Engine.hash\u secret(密码,user.password\u salt)
用户
其他的
无
结束
结束
def加密密码
如果密码存在?
self.password\u salt=BCrypt::Engine.generate\u salt
self.password\u hash=BCrypt::Engine.hash\u secret(密码、密码)
结束
结束
结束
当我尝试验证密码的存在时,我得到
NoMethodError: undefined method `password' for #<User @values={:email=>"foo@bar.com"}>
NoMethodError:n的未定义方法“密码”foo@bar.com"}>
好的,明白了
我刚才补充说:
attr_accessor :password
它验证其存在,而不尝试保存到密码列。谢谢:)好的,明白了
我刚才补充说:
attr_accessor :password
它验证其存在,而不尝试保存到密码列。谢谢:)