2016-03-21 6 views
-1

私はユーザーからのデータをUIを介してデータを取得する必要のあるレールアプリを持っていますが、DBに格納することができます。しかし、私はDBから格納されたデータを取得し、いくつかの計算を実行し、グラフライブラリを使用してプロットする必要があります。Railsアプリケーションでの計算ロジック

計算ロジックは一般的にモデルに属しているので、私のモデルファイルコードは以下の通りです。しかし、今私はどのように結果の配列をビューに渡してプロットするのかと思っています。基本的に私は相互接続権を得ていません。これについての助けがあれば幸いです。

class Shot < ActiveRecord::Base 
    def self.calculate(param) 
    #fetch_all_three_shots 
    #Performs Query aginst DB and returns the same data structure depicted in 'shots' array 
    #shots = Shot.all.collect{|a| [a.c1,a.c2,a.c3,a.c4,a.c5,a.c6,a.c7,a.c8,a.c9,a.c10,a.c11,a.c12]} 
    #shots = [[1.55, 1.58, 1.59, 1.58, 1.53, 1.57, 1.57, 1.55, 1.57, 1.6, 1.62, 1.6], 
    #  [1.54, 1.55, 1.58, 1.57, 1.49, 1.56, 1.55, 1.55, 1.54, 1.59, 1.61, 1.6], 
    #  [1.55, 1.57, 1.59, 1.57, 1.51, 1.56, 1.56, 1.55, 1.56, 1.59, 1.62, 1.59]] 
    shots = fetch_all_three_shots 

    #Declaration of Array for all Data Sets Goes here... 
    cavity_arr = Array.new 
    avgpartwt_arr = Array.new 
    fillseq_arr = Array.new 
    imbalance_arr = Array.new 
    max_imbalance_arr = Array.new 
    intermediate_arr = Array.new 
    fillmeanwt_arr = Array.new 
    statistics_shotwise_arr = Array.new 
    statistics_bof_arr = Array.new 

    #Formulation of Cavity Array Goes Here... 
    cavity_arr = (1 .. 12).to_a 

    #Looping for Calculation of Average Part Weight Goes Here... 
    for i in (0 .. (shots[0].size - 1)) 
     avgpartwt_arr.push((shots[0][i] + shots[1][i] + shots[2][i])/3) 
    end 

    #Rank Calculation Logic Goes Here.... 
    fillseq_arr = avgpartwt_arr.map{|value| avgpartwt_arr.select{|item| item > value}.size + 1} 

    #Looping for Calculation of Percentage Imbalance Goes Here... 
    for i in (0 .. (avgpartwt_arr.size - 1)) 
     imbalance_arr.push(((avgpartwt_arr.max - avgpartwt_arr[i])/avgpartwt_arr.max) * 100) 
    end 

    #Looping for Maximum Imbalance Array Goes Here... 
    for i in (0 .. (imbalance_arr.size - 1)) 
     if imbalance_arr[i] != imbalance_arr.max 
      max_imbalance_arr.push(0) 
     else 
      max_imbalance_arr.push(cavity_arr[i]) 
     end 
    end 

    #Formulation of Intermediate Array Goes Here... 
    intermediate_arr = [((avgpartwt_arr.mean).round(3))] * 12 

    #Formulation of Fill Weight Mean Array Goes Here... 
    for i in (0 .. (avgpartwt_arr.size - 1)) 
     fillmeanwt_arr.push(((intermediate_arr[i] - avgpartwt_arr[i])/intermediate_arr[i]) * 100) 
    end 

    #Looping for Calculation of Shotwise Statistical Parameters Across Shots Goes Here... 
    for i in (0 .. (shots.size - 1)) 
     statistics_shotwise_arr.push([shots[i].max, shots[i].min, shots[i].range, shots[i].mean, shots[i].standard_deviation]) 
    end 

    #Formulation of Balance of Fill Parameters Goes Here... 
    cavmaximbalance = max_imbalance_arr.max 
    maximbalance = imbalance_arr.max 
    avgimbalance = imbalance_arr.mean 
    rangedrop = (((statistics_shotwise_arr[0][2] - statistics_shotwise_arr[2][2])/statistics_shotwise_arr[0][2]) * 100) 

    end 
    scope :fetch_all_three_shots , -> {all.collect{|a| [a.c1, a.c2, a.c3, a.c4, a.c5 ,a.c6, a.c7, a.c8, a.c9, a.c10, a.c11, a.c12]}} 
end 

答えて

0

あなたが次のことを試みることができる:

class SomeController < ApplicationController 

    def some_action 
    @some_variable = Shot.calculate(params) 

    render :show_action 
    end 
end 

@some_variableShot.calculate(params)の戻り値である、ビューで使用可能です。

+0

こんにちはDharam、私はモデルファイルで直接行ったように配列を宣言できますか? 2番目の質問は私のfetch_all_three_shotsが現在のフォームでDBからデータを取得し、それを私の 'ショット'変数で利用できるようにすることでしょうか?私はレールの初心者ですから、このnoobの質問です。 –

+0

はい、モデルファイルで宣言したように '配列'を宣言することができます。しかし、このメソッドから返されるのは、メソッドの最後の行である 'rangedrop'だけです。 'ruby'は暗黙のうちにメソッドの最後の文を返します。だから、それらの配列が利用できるようにするには、[cavity_arr、avgpartwt_arr、...]のような最後のステートメントとして宣言する必要があります。 – Dharam

+0

'fetch_all_three_shots'はスコープで、' rails'はクラスメソッドとして利用できますし、クラスメソッドとして 'calculate'を宣言しても問題はありません。全体的には、これは良い設計ではないかもしれませんが、テストするのが難しい、あまりにも多くのことを行う1つの方法があるためです。 – Dharam

関連する問題