我使用
attr_encrypted
gem 加密了表中的一个字段。现在我想查询那个特定的列,将它与我从表单中检索的值进行比较。我该怎么做?
编辑:我需要查询许多加密列。例如:搜索
encrypted_email
、encrypted_name
等(在 where
子句中使用 OR 条件)
attr_encrypted
拦截 find_by
方法,所以你应该可以这样做:
class User < ActiveRecord::Base
attr_encrypted :email, :key => 'a secret key'
attr_encrypted :password, :key => 'some other secret key'
end
User.find_by_email_and_password('[email protected]', 'testing')
重写为
User.find_by_encrypted_email_and_encrypted_password('ENCRYPTED EMAIL', 'ENCRYPTED PASSWORD')
class User < ActiveRecord::Base
attr_encrypted :email, :key => 'a secret key'
end
如果您想编写一个查询来检索其电子邮件为“[email protected]”的用户,那么您可以执行任一操作
User.where(encrypted_email: User.encrypt_email('[email protected]'))
或
User.scoped_by_email('[email protected]') # works only for dynamic scope methods