2017-10-07 76 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

相關問題