ruby-on-rails - “find_by”和 'where' 不适用于 attr_encrypted

标签 ruby-on-rails

用户模型中的电子邮件字段使用 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/

相关文章:

mysql - rails 3 中的关联关联 1 个调查给 1 个用户

ruby-on-rails - 如何要求具有相同名称的不同 gem ?

ruby-on-rails - 提高加载一长串 Facebook 好友的性能

sql - 更好的 SQL - :group vs. :select => 'DISTINCT'

ruby-on-rails - ActiveRecord counter_cache 使用 multi_db gem 提供过时计数

ruby-on-rails - 如何让变量保留 2 位小数

ruby-on-rails - rails 4 - 缺少参数或值为空 : projecttype

ruby-on-rails - Rails 迁移顺序和 Git

ruby-on-rails - 如何连接到redis-rb中的集群Azure Redis缓存?

ruby-on-rails - 对象属性的 ruby 计数