【发布时间】:2026-01-15 07:15:01
【问题描述】:
我已经使用 attr_encrypted gem 加密了表中的一个字段。现在我想查询该特定字段,并将其与我从表单中检索的值进行比较。我该怎么做?
编辑:我需要查询一些加密字段。例如:搜索 encrypted_email、encrypted_name 等(在 where 子句中使用 OR 条件)
【问题讨论】:
标签: mysql ruby-on-rails ruby-on-rails-3
我已经使用 attr_encrypted gem 加密了表中的一个字段。现在我想查询该特定字段,并将其与我从表单中检索的值进行比较。我该怎么做?
编辑:我需要查询一些加密字段。例如:搜索 encrypted_email、encrypted_name 等(在 where 子句中使用 OR 条件)
【问题讨论】:
标签: mysql ruby-on-rails ruby-on-rails-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('test@example.com', '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
如果您想编写查询来检索电子邮件为“abc@xyz.com”的用户,那么您可以这样做
User.where(encrypted_email: User.encrypt_email('abc@xyz.com'))
或
User.scoped_by_email('abc@xyz.com') # works only for dynamic scope methods
【讨论】:
per_attribute_iv_and_salt 模式,使用带有encrypt_email 属性方法的where 范围进行搜索是否有效?