0
顯然我在這裏錯過了一些明顯的東西,但會很感激一個簡單的例子。如何通過ActiveRecord行結果中的屬性名稱訪問字段?
我有一條記錄從ActiveRecord回來,我從查詢中選擇了幾列。這些字段是BigDecimal格式,需要將其平鋪爲字符串。
我最初以爲是足夠拉查詢有:
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
然而,這會產生這樣的錯誤的can't convert Symbol into Integer
。顯然,我沒有完全遵循如何從行集結果中的記錄中訪問列。
您通常會如何做到這一點?
是的,我從一個我遵循的例子中不經意地離開了zip方法。如果你能看一下以下內容,我會非常好奇和讚賞,因爲這是一箇中間問題,但毫無結果,邁出了這個最終問題的一步:[如何將科學數據格式化爲圖表顯示的正確數據系列](http:// stackoverflow.com/questions/8754486/how-to-format-scientific-data-into-proper-data-series-for-graph-display-in-ruby) – ylluminate 2012-01-09 01:52:31