2017-10-07 7 views
0

私の名前はGokulで、私はBlackboard(Virtual Learning Environment)で働いています。私は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更新

、私は次のような何かを行うと、以下の結果が得られます。

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_IDを返さないため

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の一種であります生徒の成績と生徒の身分を合わせるための余分な体操。可能であれば、そのメソッドをリファクタリングする必要があります。

+0

エラーが発生した行番号とエラーをスローするコードを識別するコンソールからの出力を質問に追加してください。 – jvillian

+0

それが配列かハッシュかどうかを調べ、それに応じて処理するだけです。 – jvillian

+0

このコードはコントローラにありますか?モデル?普通のルビーオブジェクト?元の質問にクラスコードを載せてください。それを行う方法を教えてください。 – jvillian

関連する問題