Apparently I’m missing something obvious here, but would appreciate a quick example.
I’ve got a record coming back from ActiveRecord where I have a few columns selected from a query. The fields are of BigDecimal format and need to be flattened to string.
I had initially thought it was sufficient to pull the query with:
rows = ModelName.order("date DESC").select('table_name.precise_number1, table_name.precise_number2').limit(100).all.zip
rows_stringified1 = Array.new
rows_stringified2 = Array.new
readings.each do |row|
rows_stringified1.push row[:precise_number1].to_s
rows_stringified2.push row[:precise_number2].to_s
end
However, this yields an error such as can't convert Symbol into Integer. Obviously I’m not following exactly how to access columns from records in a row set result.
How would you normally do this?
Presumably you have a typo and you’re doing this:
instead of assigning to
rows. Noticed thatzipat the end? That doesn’t make any sense. When you do this:you get this:
So, in your
readings.eachblock, therowis in fact[model]rather than themodelthat you think it is and that means thatrow[:precise_number1]is trying to access the Arrayrowusing the Symbol:precise_number1instead of the Integer that the Array expects, hence your “can’t convert Symbol into Integer” error.So either get rid of the
zipand leave youreachas-is:or keep the
zipand adjust youreachblock to match whatrowreally looks like:I’d recommend getting rid of the
zipas it does nothing useful and just confuses things.