I have a query in my ruby file :
@mastertest = connection.execute("select code_ver,date from mastertest")
And I print the result of the query as below:
@mastertest.each do |row|
puts row[0] : row[1]
end
This will print all the code_ver and ‘date’ which looks like this
2.0 : 2012/12/10
3.1 : 2012/11/03
2.5 : 2012/07/08
1.8 : 2012/12/11
2.5 : 2012/03/01
Now I want to sort this array based on my code_ver, but the problem is ruby does not consider this as an array, it says it is some mysql2 type.
How do I proceed further? I want to either convert this thing to 2-d array or I would want to sort it based on the row[0].
You’re getting a MySQL result set object in
@mastertestand a result set is not an array.If you’re using
mysql2, then you should have aMysql2::Resultand that’sEnumerableso it has ato_amethod:If you’re using the
mysqlgem, then you should have aMysql::Resultand you’ll have to convert@mastertestto an Array by hand:Or you could simply let the database do the sorting: