This works! BUT if my JSON returns 200 programs, this will also print all those 200 programs. BUT I want to change the helper method in a way that it only return TOP 5 that their ercentage is the highest.
这有效!但如果我的JSON返回200个程序,这也将打印所有这200个程序。但是我想以一种只返回TOP 5的方式更改辅助方法,即它们的百分比最高。
How Can I modify my helper method to achieve this?
如何修改辅助方法来实现此目的?
P.S: I know probably the best and professional way of doing this is working with "scope" and "order",etc in the Model files or the ActiveRecord query interfaced that actually creates the JSON...but that is for now way out of my leauge! Wanted to get this done with baby steps...that for later...
PS:我知道这样做的最好和最专业的方法是在模型文件中使用“范围”和“顺序”等,或者实际创建JSON的ActiveRecord查询接口...但是现在这样做了我的感觉!希望通过婴儿步骤完成这一步......以后再...
2 个解决方案
#1
2
try the following
尝试以下方法
def patient_counts(program)
sorted = program.patient_counts.sort { |a, b| b.money <=> a.money }
sorted[0..4]
end
UPDATE: limiting to just 5 records fetched from the db
更新:限制为从db获取的5条记录
def patient_counts(program)
program.registry_patient_counts.limit(5).order('patient_count_percentage DESC')
end
#2
1
Create a hash with patients as the key, and patients.money as the value. This could be accomplished with something like:
创建以患者为关键字的哈希值,并将patients.money作为值。这可以通过以下方式实现:
unsorted_patients = { }
program.patient_counts.each do |patient|
sorted_patients[patient] = patient.money
end
Then use sort_by to sort the hash into a 2d array:
然后使用sort_by将哈希值排序为二维数组:
sorted_patients = unsorted_patients.sort_by { |patient, money| money }
sorted_patients.reverse!
The first five elements of sorted_patients will now be 2-element arrays containing the five patients with the most money, and how much money they have, something like this:
sorted_patients的前五个元素现在将是包含5个钱最多的患者的2元素数组,以及他们有多少钱,如下所示:
[[<Bob>, 150000], [<Mary>, 138000], [<Joe>, 125000],...]
You should be able to easily extract the needed information from that.
您应该能够轻松地从中提取所需的信息。
#1
2
try the following
尝试以下方法
def patient_counts(program)
sorted = program.patient_counts.sort { |a, b| b.money <=> a.money }
sorted[0..4]
end
UPDATE: limiting to just 5 records fetched from the db
更新:限制为从db获取的5条记录
def patient_counts(program)
program.registry_patient_counts.limit(5).order('patient_count_percentage DESC')
end
#2
1
Create a hash with patients as the key, and patients.money as the value. This could be accomplished with something like:
创建以患者为关键字的哈希值,并将patients.money作为值。这可以通过以下方式实现:
unsorted_patients = { }
program.patient_counts.each do |patient|
sorted_patients[patient] = patient.money
end
Then use sort_by to sort the hash into a 2d array:
然后使用sort_by将哈希值排序为二维数组:
sorted_patients = unsorted_patients.sort_by { |patient, money| money }
sorted_patients.reverse!
The first five elements of sorted_patients will now be 2-element arrays containing the five patients with the most money, and how much money they have, something like this:
sorted_patients的前五个元素现在将是包含5个钱最多的患者的2元素数组,以及他们有多少钱,如下所示:
[[<Bob>, 150000], [<Mary>, 138000], [<Joe>, 125000],...]
You should be able to easily extract the needed information from that.
您应该能够轻松地从中提取所需的信息。