我叫Gokul,我爲Blackboard(虛擬學習環境)工作。我在我的組織中需要一個新的需求,我需要開發Rails應用程序,並且在將數據存儲到模型中時遇到一些挑戰,我需要一些幫助。我是Rails的新手,所以如果我的問題聽起來很愚蠢,請道歉。將數據添加到模型中?
我們有一個實例方法(student_mark),它將參數作爲輸入並生成哈希數組。
=> "[{\"TT (Theory Total)\":{\"Mathematics\":\"89.35\",\"Physics\":\"125.5\",\"Biology\":\"96.2\",\"Data Entry Operations\":\"49.5\",\"Chemistry\":\"35.55\",\"Sanskrit\":\"40.25\"},\"PT (Practical Total)\":{\"Physics\":\"150.55\",\"Library and Information Science\":\"177.85\",\"Chemistry\":\"125.55\",\"Home Science\":\"165.45\",\"Geography\":\"188.30\",\"Computer Science\":\"195.55\"}},{\"TT (Theory Total)\":{\"Mathematics\":\"69.35\",\"Physics\":\"127.5\",\"Biology\":\"196.2\",\"Data Entry Operations\":\"99.5\",\"Chemistry\":\"87.55\",\"Sanskrit\":\"89.25\"},\"PT (Practical Total)\":{\"Physics\":\"189.55\",\"Library and Information Science\":\"198.85\",\"Chemistry\":\"145.55\",\"Home Science\":\"145.45\",\"Geography\":\"132.30\",\"Computer Science\":\"112.55\"}}]"
#新更新
截至目前,我像做以下,並得到下面的結果。
VLE :028 > theory_total_params = parsed[0]["TT (Theory Total)"].inject({}) do |to_return ,v|
VLE :029 > to_return[v[0].gsub(" ","_").downcase.to_sym] = v[1].to_f
VLE :030?> to_return
VLE :031?> end
=> {:mathematics=>89.35, :physics=>125.5, :biology=>96.2, :data_entry_operations=>49.5, :chemistry=>35.55, :sanskrit=>40.25}
VLE :032 > theory_total_params = parsed[1]["TT (Theory Total)"].inject({}) do |to_return ,v|
VLE :033 > to_return[v[0].gsub(" ","_").downcase.to_sym] = v[1].to_f
VLE :034?> to_return
VLE :035?> end
=> {:mathematics=>69.35, :physics=>127.5, :biology=>196.2, :data_entry_operations=>99.5, :chemistry=>87.55, :sanskrit=>89.25}
我的最終目標是將上述結果存儲到模型中。有了上述的東西,就不可能存儲所有的值。所以我相信我們需要迭代數組來獲得所有結果。有人可以幫助我瞭解我們如何實現它嗎?
你能列出你到底面臨的是什麼問題嗎?你有什麼試圖存儲數據,你在哪裏得到一個問題? – Aks
它是否總是*返回散列數組?或者它*有時*返回散列數組*有時*返回散列值? – jvillian
@jvillian,所以它總是返回哈希數組。 –