You can also save an additional secret_hash secret key. If two entries have the same secret , then they will have the same secret_hash .
Add something like the following to your model:
scope :by_secret, ->(secret) { where(secret_hash: Digest::MD5.hexdigest(secret) } before_save :generate_secret_hash private def generate_secret_hash self.secret_hash = Digest::MD5.hexdigest(secret) end
After that, you can request the following:
YourModel.by_secret('abd123').count
Warning
Keeping MD5 hashes of passwords and other sensitive information is a security risk. Even if you cannot specify plain secret text with secret_hash , it allows you to tell when users use the same secret . Or - even worse - an MD5 hash may be available in the MD5 reverse lookup dictionary.
You must carefully exchange this security issue in order to be able to query this column.
source share