Rails 3.1 查询使用 attr_encrypted gem 加密的数据库(加密列上的 where 子句)

问题描述 投票:0回答:2

我使用

attr_encrypted
gem 加密了表中的一个字段。现在我想查询那个特定的列,将它与我从表单中检索的值进行比较。我该怎么做?

编辑:我需要查询许多加密列。例如:搜索

encrypted_email
encrypted_name
等(在
where
子句中使用 OR 条件)

mysql ruby-on-rails ruby-on-rails-3
2个回答
3
投票

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')

2
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.