Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道抛出';列必须以';无明显理由的例外_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 轨道抛出';列必须以';无明显理由的例外

Ruby on rails 轨道抛出';列必须以';无明显理由的例外,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,情况如下: 当在psql中运行或使用ActiveRecord::Base.connection.execute SELECT SUM(loan_investments.amount) AS total_balance, COUNT(DISTINCT loan_investments.loan_id) AS number_of_loans, AVG(loan_investments.interest_rate) AS average_interest_rate,

情况如下: 当在
psql
中运行或使用
ActiveRecord::Base.connection.execute

SELECT SUM(loan_investments.amount) AS total_balance,
       COUNT(DISTINCT loan_investments.loan_id) AS number_of_loans,
       AVG(loan_investments.interest_rate) AS average_interest_rate,
       AVG(DATE_PART('month', loans.last_payment_date) - DATE_PART('month', NOW())) AS average_remaining_period 
FROM "loan_investments" 
INNER JOIN "loans" ON "loans"."id" = "loan_investments"."loan_id" 
WHERE "loan_investments"."user_id" = 8 
GROUP BY loan_investments.user_id;
下面是用于生成上述SQL的范围

 scope :performance_overview, -> {
     investments = joins(:loan)
     investments = investments.select("SUM(loan_investments.amount) AS total_balance,
                                  COUNT(DISTINCT loan_investments.loan_id) AS number_of_loans,
                                  AVG(loan_investments.interest_rate) AS average_interest_rate,
                                  AVG(DATE_PART('month', loans.last_payment_date) - DATE_PART('month', NOW())) AS average_remaining_period")
     investments.group('loan_investments.user_id')
  }
运行此作用域时,会引发以下异常:

错误:“loan_investments.id”列必须出现在GROUP BY子句中,或在聚合函数中使用

这是完全出乎意料的,因为loan_investments.id甚至不在任何地方使用,当在控制台中运行此sql时,它会产生正确的结果。我猜ActiveRecord想按
loan\u investments.id
进行分组,因为我们查询model
LoanInvestment
,但我不知道这是否正确


所以问题是为什么会出现此异常?

您在最后一行缺少一个作业

scope :performance_overview, -> {
 investments = joins(:loan)
 investments = investments.select("SUM(loan_investments.amount) AS total_balance,
                              COUNT(DISTINCT loan_investments.loan_id) AS number_of_loans,
                              AVG(loan_investments.interest_rate) AS average_interest_rate,
                              AVG(DATE_PART('month', loans.last_payment_date) - DATE_PART('month', NOW())) AS average_remaining_period")
 investments = investments.group('loan_investments.user_id')

}

您是否在调试日志中查看了实际生成的SQL语句?这个错误(很可能)不是来自Rails,而是来自Postgres,这意味着生成的SQL不是您所认为的。您可能希望通过添加到末尾来检查此作用域生成的SQL。提供的SQL是我从运行中收到的。要在给定作用域上创建SQL,我不太确定为什么我需要在最后一行添加一个赋值,因为investments.group('loan\u investments.user\u id'))返回应返回的作用域关系,因为它始终是
invenstations
的一个附加项。但是,如果模型是
invenstation
并且范围在内部,则只需将元素与“.”连接在一起,而无需任何赋值。