Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Mysql::Result in ActiveRecord?

Tags:

Example:

result = ActiveRecord::Base.connection.execute("select 'ABC'") 

How can I get the 'ABC' value from result? Tried result.first without success. Thanks

p.s. Gems:

activerecord (2.3.9)
mysql (2.8.1)

like image 740
ohho Avatar asked Sep 07 '10 10:09

ohho


1 Answers

You could try it on the cosole:

 script/console # rails 2 rails console  # rails 3 

enter your code in the console and you get:

 irb> result = ActiveRecord::Base.connection.execute("select 'ABC'")  => [{0=>"ABC", "'ABC'"=>"ABC"}]  

so it you get it with

result.first[0] # or result.first['ABC'] 

result.first just returns the first row, not the first value. This row consists of a Hash with numerical and named access.

like image 78
jigfox Avatar answered Dec 15 '22 03:12

jigfox