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
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.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.