Problems using find_by_sql()


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.



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 [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at


class User < ActiveRecord::Base; end
(and after the establish connection bits)
users = User.find_by_sql query
and users[0].decrypted_key should work.


Thanks Vish, that worked perfectly!