Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby / MySQL fetching single row but still using .each?

Tags:

mysql

ruby

mysql2

I'm using the MySQL2 Ruby driver - but it seems a bit redundant having to call

result.each{ |r| puts r['name'] }

for a single row of data that is returned. There must be a simpler way to get the mysql field I want without having to use the each block?

like image 211
Shaz Amjad Avatar asked Dec 28 '12 04:12

Shaz Amjad


1 Answers

Your result should be a Mysql2::Result and that's Enumerable so you can use first (and the rest of the Enumerable goodies) on it:

puts result.first['name']
like image 128
mu is too short Avatar answered Sep 19 '22 13:09

mu is too short