I am collecting the values for a specific column from a named_scope as follows:
我正在从named_scope收集特定列的值,如下所示:
a = survey_job.survey_responses.collect(&:base_pay)
This gives me a numeric array for example (1,2,3,4,5). I can then pass this array into various functions I have created to retrieve the mean, median, standard deviation of the number set. This all works fine however I now need to start combining multiple columns of data to carry out the same types of calculation.
这给了我一个数值数组,例如(1,2,3,4,5)。然后,我可以将此数组传递给我创建的各种函数,以检索数字集的均值,中位数,标准差。这一切都很好但是我现在需要开始组合多列数据来执行相同类型的计算。
I need to collect the details of perhaps three fields as follows:
我需要收集三个字段的详细信息如下:
survey_job.survey_responses.collect(&:base_pay)
survey_job.survey_responses.collect(&:bonus_pay)
survey_job.survey_responses.collect(&:overtime_pay)
This will give me 3 arrays. I then need to combine these into a single array by adding each of the matching values together - i.e. add the first result from each array, the second result from each array and so on so I have an array of the totals.
这将给我3个阵列。然后我需要通过将每个匹配值加在一起将它们组合成单个数组 - 即添加每个数组的第一个结果,每个数组的第二个结果,依此类推,所以我有一个总数数组。
How do I create a method which will collect all of this data together and how do I call it from the view template?
如何创建一个将所有这些数据收集在一起的方法,以及如何从视图模板中调用它?
Really appreciate any help on this one...
非常感谢对此有任何帮助......
Thanks
Simon
2 个解决方案
#1
s = survey_job.survey_responses
pay = s.collect(&:base_pay).zip(s.collect(&:bonus_pay), s.collect(&:overtime_pay))
pay.map{|i| i.compact.inject(&:+) }
Do that, but with meaningful variable names and I think it will work.
这样做,但有意义的变量名称,我认为它会起作用。
Define a normal method in app/helpers/_helper.rb and it will work in the view
在app / helpers / _helper.rb中定义一个普通方法,它将在视图中工作
Edit: now it works if they contain nil or are of different sizes (as long as the longest array is the one on which zip is called.
编辑:现在它可以工作,如果它们包含nil或不同大小(只要最长的数组是调用zip的数组。
#2
Here's a method that will combine an arbitrary number of arrays by taking the sum at each index. It'll allow each array to be of different length, too.
这是一种通过获取每个索引的总和来组合任意数量的数组的方法。它也允许每个阵列具有不同的长度。
def combine(*arrays)
# Get the length of the largest array, that'll be the number of iterations needed
maxlen = arrays.map(&:length).max
out = []
maxlen.times do |i|
# Push the sum of all array elements at a given index to the result array
out.push( arrays.map{|a| a[i]}.inject(0) { |memo, value| memo += value.to_i } )
end
out
end
Then, in the controller, you could do
然后,在控制器中,你可以做到
base_pay = survey_job.survey_responses.collect(&:base_pay)
bonus_pay = survey_job.survey_responses.collect(&:bonus_pay)
overtime_pay = survey_job.survey_responses.collect(&:overtime_pay)
@total_pay = combine(base_pay, bonus_pay, overtime_pay)
And then refer to @total_pay
as needed in your view.
然后在视图中根据需要参考@total_pay。
#1
s = survey_job.survey_responses
pay = s.collect(&:base_pay).zip(s.collect(&:bonus_pay), s.collect(&:overtime_pay))
pay.map{|i| i.compact.inject(&:+) }
Do that, but with meaningful variable names and I think it will work.
这样做,但有意义的变量名称,我认为它会起作用。
Define a normal method in app/helpers/_helper.rb and it will work in the view
在app / helpers / _helper.rb中定义一个普通方法,它将在视图中工作
Edit: now it works if they contain nil or are of different sizes (as long as the longest array is the one on which zip is called.
编辑:现在它可以工作,如果它们包含nil或不同大小(只要最长的数组是调用zip的数组。
#2
Here's a method that will combine an arbitrary number of arrays by taking the sum at each index. It'll allow each array to be of different length, too.
这是一种通过获取每个索引的总和来组合任意数量的数组的方法。它也允许每个阵列具有不同的长度。
def combine(*arrays)
# Get the length of the largest array, that'll be the number of iterations needed
maxlen = arrays.map(&:length).max
out = []
maxlen.times do |i|
# Push the sum of all array elements at a given index to the result array
out.push( arrays.map{|a| a[i]}.inject(0) { |memo, value| memo += value.to_i } )
end
out
end
Then, in the controller, you could do
然后,在控制器中,你可以做到
base_pay = survey_job.survey_responses.collect(&:base_pay)
bonus_pay = survey_job.survey_responses.collect(&:bonus_pay)
overtime_pay = survey_job.survey_responses.collect(&:overtime_pay)
@total_pay = combine(base_pay, bonus_pay, overtime_pay)
And then refer to @total_pay
as needed in your view.
然后在视图中根据需要参考@total_pay。