用户模型中的电子邮件字段使用 attr_encrypted 进行加密。当我使用 devise 和 facebookomniauth 创建新用户时,它实际上保存在数据库中。另外,我可以使用 User.last.email #=> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24424b4b64424b4b0a474b49" rel="noreferrer noopener nofollow">[email protected]</a>
检索该数据。
但是当我使用 User.find_by_email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01676e6e41676e6e2f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>")
时它返回 nil 或 []或User.where(email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f4fdfdd2f4fdfdbcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>")
.
用户.rb
class User < ActiveRecord::Base
attr_encrypted :email, key: Settings.encryption.key
end
最佳答案
如果您使用 attr_encrypted
gem,我认为 email
是虚拟字段,并且实际上作为 users
表中的 持久化>加密的电子邮件
字段。
users
表的 schema.rb
文件是什么样的?
如果是这种情况,我认为你只需要使用
User.find_by_encrypted_email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1d14143b1d141455181416" rel="noreferrer noopener nofollow">[email protected]</a>")
而不是
User.find_by_email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e4ededc2e4ededace1edef" rel="noreferrer noopener nofollow">[email protected]</a>")
Gem 文档:https://github.com/attr-encrypted/attr_encrypted
祝你好运!
关于ruby-on-rails - “find_by”和 'where' 不适用于 attr_encrypted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683511/