<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> <font face="sans-serif">Hi,<br> <br> I need to use the MySQL AES_ENCRYPT & AES_DECRYPT functions in an application I''m writing. The key that is used to decrypt & encrypt information is obtained from a (also encrypted) PIN that users will enter when logging in to the site. I have found that I should be able to carry out the necessary SQL statements using the find_by_sql method in ActiveRecord::Base.<br> <br> It looks like I''m getting the required data out of the DB OK, but I cannot access any of the data contained within the ActiveRecord::Base object that is returned. Please could someone cast their eye over the console session shown below and see if they can figure out why the ''attributes'' method is showing nil for my column data, even though the correct data can be seen in the @attributes hash?<br> <br> </font><tt>>> query = "SELECT id, AES_DECRYPT(`key`, pin) AS decrypted_key FROM users WHERE id = 1"<br> => "SELECT id, AES_DECRYPT(`key`, pin) AS decrypted_key FROM users WHERE id = 1"<br> >> result = ActiveRecord::Base.find_by_sql query<br> => [#<ActiveRecord::Base:0xb767db6c @attributes={"decrypted_key"=>"test", "id"=>"1"}>]<br> >> result[0].attributes<br> => {"id"=>nil, "decrypted_key"=>nil}</tt><font face="sans-serif"><br> <br> <br> If I try to use puts to look at either the id or the decrypted_key, I get the following error:<br> <br> </font><tt>>> puts result[0].decrypted_key<br> NoMethodError: undefined method `abstract_class?'' for Object:Class<br> ...</tt><font face="sans-serif"><br> <br> Thanks in advance for any help anyone can give.<br> <br> <br> Cheers,<br> <br> Tim<br> </font> <br> --~--~---------~--~----~------------~-------~--~----~<br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. <br> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk <br> -~----------~----~----~----~------~----~------~--~---<br> </body> </html> <br>
Try class User < ActiveRecord::Base; end (and after the establish connection bits) users = User.find_by_sql query and users[0].decrypted_key should work. Vish On 9/25/06, Tim Littlemore <tim-4IXDTieS7TA@public.gmane.org> wrote:> > Hi, > > I need to use the MySQL AES_ENCRYPT & AES_DECRYPT functions in an > application I''m writing. The key that is used to decrypt & encrypt > information is obtained from a (also encrypted) PIN that users will enter > when logging in to the site. I have found that I should be able to carry out > the necessary SQL statements using the find_by_sql method in > ActiveRecord::Base. > > It looks like I''m getting the required data out of the DB OK, but I cannot > access any of the data contained within the ActiveRecord::Base object that > is returned. Please could someone cast their eye over the console session > shown below and see if they can figure out why the ''attributes'' method is > showing nil for my column data, even though the correct data can be seen in > the @attributes hash? > > >> query = "SELECT id, AES_DECRYPT(`key`, pin) AS decrypted_key FROM users > WHERE id = 1" > => "SELECT id, AES_DECRYPT(`key`, pin) AS decrypted_key FROM users WHERE > id = 1" > >> result = ActiveRecord::Base.find_by_sql query > => [#<ActiveRecord::Base:0xb767db6c @attributes={"decrypted_key"=>"test", > "id"=>"1"}>] > >> result[0].attributes > => {"id"=>nil, "decrypted_key"=>nil} > > > If I try to use puts to look at either the id or the decrypted_key, I get > the following error: > > >> puts result[0].decrypted_key > NoMethodError: undefined method `abstract_class?'' for Object:Class > ... > > Thanks in advance for any help anyone can give. > > > Cheers, > > Tim > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks Vish, that worked perfectly! Cheers, Tim Vishnu Gopal wrote:> Try > > class User < ActiveRecord::Base; end > (and after the establish connection bits) > users = User.find_by_sql query > and users[0].decrypted_key should work. > > Vish >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---