I’m in a weird situation where I’m storing encrypted information in a
database. For example, a User.name field would be stored in its
encrypted format, so its not human-readable when accessing:
=> “)@Nw0asna;sd//20b3Q#A02nsoasd \n”
(This is just an example. I’m not actually encrypting names and the
model isn’t User But you get the idea.)
What I’m trying to figure out is the right way to go about
automatically assigning the “name” method to decrypt the data itself
when it’s being pulled out for display.
For example, I tried:
self.name = decrypt_name
However, that has obvious problems - in my test, it seems to form an
infinite loop, bouncing back and forth between the “name” method and
the decrypt_name method (because one calls the other).
Is there any way other than writing specific get and set methods to do
this? I’d even be happy with a retrieval happening within find:
=> #<User id: 1, name: “John D.”>
Even though “name” stored inside the database is actually encrypted.
Any thoughts? Thanks