如何在Rails中添加记录计数组?

时间:2017-08-13 15:30:23

标签: ruby-on-rails postgresql

我有以下代码行从我的数据库中获取记录并创建一个新的合并记录:

@opportunities = @unpublished_reports.group(:assigned_user_id, :probability, :reporting_date).
                                select("assigned_user_id, 
                                SUM(amount) AS amount,
                                SUM(amount_tp) AS amount_tp,
                                probability AS probability,
                                reporting_date AS reporting_date")

这给了我一个像这样的数组(我只显示数组的第一个对象):

[#<Opportunity id: nil, assigned_user_id: "1234", amount: 1101650, amount_tp: 1548964, probability: 0, reporting_date: "2017-08-25">]

我现在要做的是在此数组中包含所有这些记录的计数。我试过这样的事情:

@opportunities = @unpublished_reports.group(:assigned_user_id, :probability, :reporting_date).
                                select("assigned_user_id, 
                                SUM(amount) AS amount,
                                SUM(amount_tp) AS amount_tp,
                                probability AS probability,
                                reporting_date AS reporting_date,
                                COUNT(assigned_user_id) AS number_of_records")

然而,这不起作用 - 我的数组看起来仍然相同。怎么做? 我正在使用postgresql。

1 个答案:

答案 0 :(得分:2)

[#<Opportunity id: nil, assigned_user_id: "1234", amount: 1101650, amount_tp: 1548964, probability: 0, reporting_date: "2017-08-25">] 被选中,正如您所期待的那样;它只是没有显示在控制台输出中:

amount

此输出中仅显示表中保留的列,因为您有amount_tp@opportunities.last.number_of_records等...列,此处为。

如果您在控制台number_of_records中尝试,您将收到预期的结果。只需在模型上调用def randomQuestions(): q1 = 1 q2 = 2 for i in range(1): if random.randint(1,2)==1: a1=int(input(print("Please Enter a Number that is not prime: ",end=""))) if a1>1: for i in range (2,a1): if (a1%i)==0: print("Correct",a1,"is Not a prime number") break else: print("Incorrect",a1,"is a Prime number") else: print("Incorrect",a1,"is a Prime number") else: a2=int(input(print("Please Enter a Number that is positive: ",end=""))) if a2>0: print("Correct",a2,"is Positive") elif a2==0: print(a2,'Is neither Positive Nor Negative') else: print('Incorrect',a2,'is Not Positive') randomQuestions() ,就像有这样的方法一样。