# Fixing column value in the model

**URL:** <https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076>\
**Category:** Rails\
**Created:** [May 3, 2009, 11:50am UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076 "2009-05-03T11:50:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 11:50am UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/1 "2009-05-03T11:50:58Z")

</div>

I’m using a Crypto.encrypt(“string”) to create a record for a column and  
Crypto.decrypt(column) when reading and presenting it in the browser. I  
do this in the controller. Can I do it in the model instead?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/2 "2009-05-03T16:01:41Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> I’m using a Crypto.encrypt(“string”) to create a record for a column and  
> Crypto.decrypt(column) when reading and presenting it in the browser. I  
> do this in the controller. Can I do it in the model instead?

Yes. You can create a custom attribute for the unencypted version,  
which will exist in memory and not be persisted. You can then use a  
Callback to encrypt and set the persisted column before an  
insert/update. Look at examples of authentication plugins and blog  
posts and you’ll see how it’s done.

Also, don’t forget to filter the parameter in the controller (e.g.  
filter\_parameter\_loggoing :password) so the form posted parameter is not  
logged in clear text, assuming you’re accepting if from a form that is.

b

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 4:34pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/3 "2009-05-03T16:34:42Z")

</div>

Brian Mr wrote:

> PÃ¥l BergstrÃ¶m wrote:
> 
> > I’m using a Crypto.encrypt(“string”) to create a record for a column and  
> > Crypto.decrypt(column) when reading and presenting it in the browser. I  
> > do this in the controller. Can I do it in the model instead?
> 
> Yes. You can create a custom attribute for the unencypted version,  
> which will exist in memory and not be persisted. You can then use a  
> Callback to encrypt and set the persisted column before an  
> insert/update. Look at examples of authentication plugins and blog  
> posts and you’ll see how it’s done.
> 
> Also, don’t forget to filter the parameter in the controller (e.g.  
> filter\_parameter\_loggoing :password) so the form posted parameter is not  
> logged in clear text, assuming you’re accepting if from a form that is.
> 
> b

I got it working with before\_save in the model, encrypting the data  
before it goes to the db. Great.

But what about before show or listing records? How can I make a similar  
decrypt? Don’t understand what to use.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/4 "2009-05-03T17:00:48Z")

</div>

Brian Mr wrote:

> PÃ¥l BergstrÃ¶m wrote:

> Simply add a public method to the model that returns the unendrypted  
> version. The method will not map to an actual column in the db, but to  
> the controller it will appear just like any other colum.
> 
> e.g.
> 
> def myattribute  
> Crypto.decrypt(column)  
> end

I don’t understand all the way. Probably a stupid question but could you  
be more specific with what you mean with"myattribute"? Is that the  
column name or?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/5 "2009-05-03T16:48:50Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> Brian Mr wrote:
> 
> > PÃ¥l BergstrÃ¶m wrote:
> > 
> > > I’m using a Crypto.encrypt(“string”) to create a record for a column and  
> > > Crypto.decrypt(column) when reading and presenting it in the browser. I  
> > > do this in the controller. Can I do it in the model instead?
> > 
> > Yes. You can create a custom attribute for the unencypted version,  
> > which will exist in memory and not be persisted. You can then use a  
> > Callback to encrypt and set the persisted column before an  
> > insert/update. Look at examples of authentication plugins and blog  
> > posts and you’ll see how it’s done.
> > 
> > Also, don’t forget to filter the parameter in the controller (e.g.  
> > filter\_parameter\_loggoing :password) so the form posted parameter is not  
> > logged in clear text, assuming you’re accepting if from a form that is.
> > 
> > b
> 
> I got it working with before\_save in the model, encrypting the data  
> before it goes to the db. Great.
> 
> But what about before show or listing records? How can I make a similar  
> decrypt? Don’t understand what to use.

Simply add a public method to the model that returns the unendrypted  
version. The method will not map to an actual column in the db, but to  
the controller it will appear just like any other colum.

e.g.

def myattribute  
Crypto.decrypt(column)  
end

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/6 "2009-05-03T17:35:46Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> Brian Mr wrote:
> 
> > PÃ¥l BergstrÃ¶m wrote:
> 
> > Simply add a public method to the model that returns the unendrypted  
> > version. The method will not map to an actual column in the db, but to  
> > the controller it will appear just like any other colum.
> > 
> > e.g.
> > 
> > def myattribute  
> > Crypto.decrypt(column)  
> > end
> 
> I don’t understand all the way. Probably a stupid question but could you  
> be more specific with what you mean with"myattribute"? Is that the  
> column name or?

It’s not a column, it’s just a method. ActiveRecord is just a Ruby  
Class, so you can add your own methods. e.g. To use the method in a  
controller:

x = myrecord.find…  
y = myrecord.myattribute

y now holds the unencypted value.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/7 "2009-05-03T22:45:16Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> Brian Mr wrote:
> 
> > PÃ¥l BergstrÃ¶m wrote:
> 
> > y = myrecord.myattribute
> > 
> > y now holds the unencypted value.
> 
> Got it working with this in the model:
> 
> before\_save :crypt\_data  
> after\_save :decrypt\_data  
> after\_find :decrypt\_data  
> define\_method(:after\_find) { }
> 
> Works perfect.
> 
> Just one more thing. How do I deal with search? I have a solution but  
> perhaps I’m not doing it right.
> 
> I have a custom decrypt on the data before the find using %string% and  
> LIKE, but it must be full words as the columns holds the encrypted data.  
> Anyway around this?

Sorry, don’t have an answer for that. If nobody else replies, you might  
want to post a new question for that. Glad the got the rest working!

b

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [May 3, 2009, 9:18pm UTC](https://www.ruby-forum.com/t/fixing-column-value-in-the-model/167076/8 "2009-05-03T21:18:00Z")

</div>

Brian Mr wrote:

> PÃ¥l BergstrÃ¶m wrote:

> y = myrecord.myattribute
> 
> y now holds the unencypted value.

Got it working with this in the model:

before\_save :crypt\_data  
after\_save :decrypt\_data  
after\_find :decrypt\_data  
define\_method(:after\_find) { }

Works perfect.

Just one more thing. How do I deal with search? I have a solution but  
perhaps I’m not doing it right.

I have a custom decrypt on the data before the find using %string% and  
LIKE, but it must be full words as the columns holds the encrypted data.  
Anyway around this?
