2017-10-07 81 views
0

我叫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} 

我的最终目标是将上述结果存储到模型中。有了上述的东西,就不可能存储所有的值。所以我相信我们需要迭代数组来获得所有结果。有人可以帮助我了解我们如何实现它吗?

+0

你能列出你到底面临的是什么问题吗?你有什么试图存储数据,你在哪里得到一个问题? – Aks

+0

它是否总是*返回散列数组?或者它*有时*返回散列数组*有时*返回散列值? – jvillian

+0

@jvillian,所以它总是返回哈希数组。 –

回答

0

考虑:

student_ids = [8, 10] 

做这样的事情:

student_ids.each do |student_id|          # for each student you're interested in... 
    score_set_hsh = JSON.parse(student_mark(student_id))    # get the score_set_hash from your student_mark method 
    [                 # convenience array to avoid repetition in following code 
    ["TT (Theory Total)", TheoryTotal], 
    ["PT (Practical Total)", PracticalTotal] 
    ].each do |extract_ary|            # iterate the convenience array 
    score_total_key = extract_ary[0]        # assign score_total_key, e.g.: "TT (Theory Total)" 
    score_total_klass = extract_ary[1]        # assign score_total_klass, e.g.: TheoryTotal 
    score_set_hsh[score_total_key]         # grab one of the score total hashes 
     .each_with_object({}) do |raw_score_ary, to_return|    # iterate through its elements 
     score_key = raw_score_ary[0].gsub(" ","_").downcase.to_sym # grab the element key, e.g.: :mathematics 
     score_value = raw_score_ary[1].to_f       # grab the element value, e.g.: 89.35 
     to_return[score_key] = score_value       # assign the element value to the return hash using the key 
     end. 
     merge!(student_id: student_id).         # add the student id 
     tap do |formatted_score_hsh| 
     new_score = score_total_klass.new(formatted_score_hsh)  # instantiate the new record 
     if new_score.valid?           # check if valid 
      new_score.save.tap do |save_result|       # save if valid 
      puts "save_result: #{save_result}"      # some outputs just to look at things 
      puts "new_score.inspect: #{new_score.inspect}"    
      end 
     else 
      #do some error handling          # if you have errors 
     end 
     end 
    end 
end 

BTW,这个方法是有点jenk的,因为它不返回在这意味着你必须做的通过student_id数据一些额外的体操将学生成绩与学生证相匹配。如果可以的话,你应该重构该方法。

+0

请在控制台输出中添加错误代码,并输出错误代码。 – jvillian

+0

只要做一个测试,看看它是一个数组还是一个散列,然后相应地处理。 – jvillian

+0

这是代码在控制器?一个模型?一个普通的红宝石物体?请在原始问题中发布课程代码,我会告诉你如何去做。 – jvillian

相关问题