Rails 3.1 Qeurying database encrypted with attr_encrypted gem (where clause in encrypted fields)

I encrypted the field in the table using the attr_encrypted attribute. Now I want to request in this particular field a comparison of it with the value that I am extracting from the form. How can i do this?

EDIT . I need to request some encrypted fields. For example: search on encrypted_email, encrypted_name, etc. (Using the OR clause in the where clause)

+6
source share
2 answers

attr_encrypted intercepts the attr_encrypted methods, so you must 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(' test@example.com ', 'testing') 

This is written as

 User.find_by_encrypted_email_and_encrypted_password('ENCRYPTED EMAIL', 'ENCRYPTED PASSWORD') 
+3
source
 class User < ActiveRecord::Base attr_encrypted :email, :key => 'a secret key' end 

If you want to write a request to get a user whose email address is " abc@xyz.com ", you can do either

 User.where(encrypted_email: User.encrypt_email(' abc@xyz.com ')) 

or

 User.scoped_by_email(' abc@xyz.com ') # works only for dynamic scope methods 
+2
source

Source: https://habr.com/ru/post/906069/


All Articles