I have encrypted a field in the table using the attr_encrypted gem. Now I want to query on that particular field comparing it with a value I am retrieving from a form. How can I do this?
EDIT : I need to query on a number of encrypted fields. Eg: searching on encrypted_email, encrypted_name etc. (using OR condition in where clause)
attr_encrypted
intercepts find_by
methods, so you should be able to do this:
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')
This is rewritten as
User.find_by_encrypted_email_and_encrypted_password('ENCRYPTED EMAIL', 'ENCRYPTED PASSWORD')
class User < ActiveRecord::Base
attr_encrypted :email, :key => 'a secret key'
end
If you want to write a query to retrieve user whose email is '[email protected]' then you can do either
User.where(encrypted_email: User.encrypt_email('[email protected]'))
or
User.scoped_by_email('[email protected]') # works only for dynamic scope methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With