# How do I automatically modify a method when accessing it?

**URL:** <https://www.ruby-forum.com/t/how-do-i-automatically-modify-a-method-when-accessing-it/165270>\
**Category:** Rails\
**Created:** [April 11, 2009, 3:14am UTC](https://www.ruby-forum.com/t/how-do-i-automatically-modify-a-method-when-accessing-it/165270 "2009-04-11T03:14:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [April 11, 2009, 3:14am UTC](https://www.ruby-forum.com/t/how-do-i-automatically-modify-a-method-when-accessing-it/165270/1 "2009-04-11T03:14:57Z")

</div>

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:

User.name  
=\> “)@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:

def name  
self.name = decrypt\_name  
end

private  
def decrypt\_name  
decryption\_object.decrypt(self.name)  
end

* * *

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.find(:first)  
=\> #\<User id: 1, name: “John D.”\>

Even though “name” stored inside the database is actually encrypted.

Any thoughts? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [April 11, 2009, 3:26am UTC](https://www.ruby-forum.com/t/how-do-i-automatically-modify-a-method-when-accessing-it/165270/2 "2009-04-11T03:26:09Z")

</div>

Okay, I think I’m on the right track.

I just found out that ActiveRecord::Base has a section on overwriting  
default accessors. So my basic method is now:

def name  
decrypt(read\_attribute(:name))  
end

The only thing that concerns me is that I also have a  
before\_save :encrypt\_data call as part of the class definition. So  
let’s say I submit a form via post:

@user = User.new(params[:user])  
@user.save

If I read this right, when I call User.new, or if I were to specify  
User.find(:first, :conditions =\> {:name =\> “John D.”}), it would  
first try to decrypt data _that isn’t encrypted in the first place_).

Am I way off track here or is this about right, and if so, is there  
any way I can automate the abstraction that needs to take place, so  
that User.name, when accessed, is decrypted only if it’s in an  
encrypted form?

Hope that makes sense 🙂
