我有一个rails应用程序,其中我需要通过UI从用户获取数据将它们存储在数据库中,我可以做到没有任何打嗝。但是,我需要从DB检索存储的数据并对它们执行一些计算,然后使用图形库绘制它们。
我读到计算逻辑通常属于模型,因此我的模型文件代码如下所示。但现在我想知道如何将我的结果数组传递给视图并绘制它。基本上我没有得到正确的互连。对此有任何帮助将非常感激。
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 :(得分:0)
您可以尝试以下方法:
class SomeController < ApplicationController
def some_action
@some_variable = Shot.calculate(params)
render :show_action
end
end
@some_variable
是Shot.calculate(params)
的返回值,在视图中可用。