# After\_(read|find) callback?

**URL:** <https://www.ruby-forum.com/t/after-read-find-callback/65831>\
**Category:** Rails\
**Created:** [February 11, 2006, 11:33am UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831 "2006-02-11T11:33:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 11:33am UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/1 "2006-02-11T11:33:31Z")

</div>

I am pondering the possibility of encrypting/decrypting some fields  
in a SQLite backend on-the-fly.

The point of the message is not security, I know that’s broken, but  
whether there’s a technique that provides on-the-fly save/read  
filters. Of course the solution would need to work transparently in  
joins, so

```
user.posts.last.title

```

would do the right thing if title was an encrypted field.

I see in the documentation of ActiveRecord::Callbacks there’s a  
before\_save callback that looks like going in the right direction,  
but I don’t see the symmetric after\_(read|find). Any ideas?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/2 "2006-02-11T12:08:07Z")

</div>

Why not just write a method that gives you the unencrypted password?

def clear\_title  
cool\_unencryption\_algorithm title  
end

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/3 "2006-02-11T12:17:50Z")

</div>

On Feb 11, 2006, at 12:07, Pat M. wrote:

> Why not just write a method that gives you the unencrypted password?
> 
> def clear\_title  
> cool\_unencryption\_algorithm title  
> end

I would need to write too much code, and violate DRY. Roughly what I  
have in mind is:

```
class RootModelClass < ActiveRecord::Base
  before_save do |obj|
    for all attributes in obj
      if attribute does not end with "id"
        encrypt attribute
      end
    end
  end

  after_read do |obj|
    for all attributes in obj
      if attribute does not end with "id"
        decrypt attribute
      end
    end
  end
end

```

And then all my models would inherit from RootModelClass.

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/4 "2006-02-11T12:49:12Z")

</div>

On Feb 11, 2006, at 3:17 AM, Xavier N. wrote:

> ```
> for all attributes in obj
> if attribute does not end with "id"
> decrypt attribute
> end
> end
> end
> 
> ```
> 
> end
> 
> And then all my models would inherit from RootModelClass.

Check out Sentry.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/5 "2006-02-11T13:57:43Z")

</div>

Hi Xavier,

On 11 Feb 2006, at 10:33, Xavier N. wrote:

> would do the right thing if title was an encrypted field.
> 
> I see in the documentation of ActiveRecord::Callbacks there’s a  
> before\_save callback that looks like going in the right direction,  
> but I don’t see the symmetric after\_(read|find). Any ideas?

There is some code which does exactly what you are after, on pp.  
268-270 (277-279 in the PDF) of Agile Development with Rails.

Too much to type out here, but basically you end up with a neat new  
addition to ActiveRecord::Base that lets you do this:

class Order \< ActiveRecord::Base  
encrypt :name, :email  
end

The callback methods you need to hook into are before\_save,  
after\_save and after\_find.

Jon

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [February 11, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/after-read-find-callback/65831/6 "2006-02-11T14:49:21Z")

</div>

On Feb 11, 2006, at 13:56, Jon Evans wrote:

> The callback methods you need to hook into are before\_save,  
> after\_save and after\_find.

Great. I readed the Agile from cover to cover, but had completely  
forgottten that example. I’ll probably delegate this stuff to Sentry  
(thank you Tom!), but nevertheless I wonder why after\_find is not  
listed in the left-bottom box of [http://api.rubyonrails.org/](http://api.rubyonrails.org/).

– fxn
