我正在开发一个有申请人和答案的应用程序。协会:
applicant has_many :answers
我想只返回答案未归档的申请人(在答案表中,已存档:false)。目前,这是我的问题:
def index
@super_admin = current_admin.super_admin
@applicants = Applicant.joins(:answers)
.where(answers: {archived: false})
.group("applicant_id")
end
查询运行正常,但我在视图中遇到了麻烦。我想列出查询返回的所有申请人的ID,并链接到该申请人的答案(由他们的answers.applicant_id找到)。我在索引操作中添加了.group,因为没有它,我得到的每个答案都没有归档(而不是每个答案都没有归档的申请人)。因此,对于申请人4,我希望看到一个链接'4'来导致他们的所有答案。 这是视图中的链接:
<% @applicants.each do |a| %>
<%= link_to "#{a.applicant_id}", "/admins/view/#{a.applicant_id}" %><br>
<% end %>
它抛出了这个错误:
PG::GroupingError: ERROR: column "applicants.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "applicants".* FROM "applicants" INNER JOIN "answers"...
^
: SELECT "applicants".* FROM "applicants" INNER JOIN "answers" ON "answers"."applicant_id" = "applicants"."id" WHERE "answers"."archived" = $1 GROUP BY applicant_id
有什么想法?谢谢!!!
答案 0 :(得分:0)
我找到了一个解决方法(它不是那么光滑,但明天会让我通过演示文稿):
def index
@super_admin = current_admin.super_admin
@ids = Applicant.all
@applicants = Applicant.joins(:answers)
.where(answers: {archived: false})
.group("applicant_id")
end
视图:
<% @ids.each do |a| %>
<%= link_to "#{a.id}", "/admins/view/#{a.id}" %><br>
<% end %>