[英]Rails 3.1 Qeurying the database encrypted with attr_encrypted gem (where clause on encrypted fields)
我使用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('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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.