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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 19, 9:11 pm, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> 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> > David-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ps rails 2.3.5 ruby 1.8.7 On 7/19/10, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Sent from my mobile device -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Mon, Jul 19, 2010 at 5:06 PM, DK <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> Ps rails 2.3.5 ruby 1.8.7 > > On 7/19/10, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > 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 > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > > For more options, visit this group at > > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > -- > Sent from my mobile device > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Maybe Matching Threads
- Calling a method directly from a test => no method error
- Did rails or shoulda go insane on the inflection of 'taxes'?
- Delaying initialization of associations until first access
- Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
- Date parse - month and day reversed