我叫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,所以它总是返回哈希数组。 –