# ActiveRecord override

**URL:** <https://www.ruby-forum.com/t/activerecord-override/191859>\
**Category:** Rails\
**Created:** [July 19, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/activerecord-override/191859 "2010-07-19T22:13:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 19, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/activerecord-override/191859/1 "2010-07-19T22:13:34Z")

</div>

Would really appreciate it if someone could tell me why this is  
happening:

I override ssn method of AR class so I can encrypt/decrypt to/from the  
db:

class Person \< ActiveRecord::Base  
def ssn=(value)  
write\_attribute(:borrower\_ssn, Crypto.encrypt(self.encryption\_key,  
value))  
write\_attribute(:borrower\_ssn\_final\_four, value[5,4])  
end

def ssn  
# this begin - rescue can be removed once all data is encrypted  
begin  
return Crypto.decrypt(self.encryption\_key,  
read\_attribute(:borrower\_ssn)) if read\_attribute(:borrower\_ssn) &&  
self.encryption\_key  
rescue  
return read\_attribute(:borrower\_ssn) if  
read\_attribute(:borrower\_ssn)  
&& self.encryption\_key  
end  
nil  
end  
end

However, when I do Person.find(27) I get:

#\<Person id: 27, ssn: “l+IV+c55qywTjPd+bW+IGA==\n” \>

And then if I do p = Person.find(27), then I get the decrypted ssn.

I know that find must be processing under the override methods, but I  
dont  
understand why, as I thought if you overrode a method it would hold in  
all  
situations for the AR object.

David

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 19, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/activerecord-override/191859/2 "2010-07-19T22:44:01Z")

</div>

On Jul 19, 9:11 pm, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, when I do Person.find(27) I get:
> 
> #\<Person id: 27, ssn: “l+IV+c55qywTjPd+bW+IGA==\n” \>
> 
> And then if I do p = Person.find(27), then I get the decrypted ssn.
> 
> I know that find must be processing under the override methods, but I dont  
> understand why, as I thought if you overrode a method it would hold in all  
> situations for the AR object.

inspect ( which is how objects are displayed by the console ) uses  
read\_attribute when building up its representation of the object

Fred

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 19, 2010, 11:08pm UTC](https://www.ruby-forum.com/t/activerecord-override/191859/3 "2010-07-19T23:08:37Z")

</div>

Ps rails 2.3.5 ruby 1.8.7

On 7/19/10, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> nil
> 
> ```
> 
> understand why, as I thought if you overrode a method it would hold in all  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Sent from my mobile device

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 20, 2010, 1:32am UTC](https://www.ruby-forum.com/t/activerecord-override/191859/4 "2010-07-20T01:32:37Z")

</div>

Thanks Frederick… I see what you are saying. So I learned something  
new,  
and it turns out the actual issue was that I forgot to run the migration  
where I munged cleartext in to encrypted.
