Reputation: 2430
I have a query in my Ruby file:
@mastertest = connection.execute("select code_ver, date from mastertest")
It treats @mastertest
like a 2-D array because, when I print the rows, I get:
@mastertest.each do |row|
puts row[0] : row[1]
end
This prints all code_ver
and date
from each row.
I am not able to perform any other operations on it. I cannot sort the array nor can I perform a deep copy of the array. I'm guessing it is some MySQL2 type which Ruby considers. How do I convert this to a normal 2-D array?
Upvotes: 1
Views: 2000
Reputation: 13067
The class of @mastertest is Mysql2::Result
; it provides only each
and fields
methods
Here's an example for one way to convert the results into a 2-D array:
sql = "select <field1>, <field2> from <table> where <conditions>"
result_array = []
result = ActiveRecord::Base.connection.execute(sql)
index = 0
result.each do |row|
result_array[index] = []
result_array[index] << row[0]
result_array[index] << row[1]
...
result_array[index] << row[n]
...
index += 1
end
Upvotes: 4