将数据添加到模型中?

时间:2017-10-07 19:59:14

标签: ruby-on-rails

我的名字是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\"}}]"

#New Update

截至目前,我执行以下操作并获得以下结果。

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}

我的最终目标是将上述结果存储到模型中。有了上述内容,就无法存储所有值。所以我认为我们需要迭代数组以获得所有结果。有人可以帮助我了解我们如何实现它吗?

1 个答案:

答案 0 :(得分: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
顺便说一下,这种方法有点像jenk,因为它不会返回传递的student_id,这意味着你必须做一些额外的体操来匹配学生成绩和学生ID。如果可以的话,你应该重构那种方法。