# Problems using find\_by\_sql()

**URL:** <https://www.ruby-forum.com/t/problems-using-find-by-sql/69951>\
**Category:** Rails\
**Created:** [September 25, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/problems-using-find-by-sql/69951 "2006-09-25T19:43:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Littlemore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@Tim\_Littlemore](https://www.ruby-forum.com/u/Tim_Littlemore)\
**Post date:** [September 25, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/problems-using-find-by-sql/69951/1 "2006-09-25T19:43:26Z")

</div>

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 removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk   
 -~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![Tim\_Littlemore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@Tim\_Littlemore](https://www.ruby-forum.com/u/Tim_Littlemore)\
**Post date:** [September 25, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/problems-using-find-by-sql/69951/2 "2006-09-25T20:03:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tim\_Littlemore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@Tim\_Littlemore](https://www.ruby-forum.com/u/Tim_Littlemore)\
**Post date:** [September 25, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/problems-using-find-by-sql/69951/3 "2006-09-25T22:11:55Z")

</div>

Thanks Vish, that worked perfectly!

Cheers,

Tim
