简体   繁体   中英

Ruby on Rails - PG Error

On Rails 4, hosted through Heroku. I've read a few threads about this problem but needed to make my own for clarification. Relevant models:

Submission:

  belongs_to :category
  belongs_to :user
  belongs_to :organization
  belongs_to :division

In the submissions' index page, I list all the submissions made by the user, grouping by the :contest_year attribute and also the :organization_id attribute that the submission belongs to.

Submission Controller:

def index
  @paginate = current_user.submissions.group([:organization_id, 
  :contest_year]).includes(:organization).page(params[:page]).per(3).order('contest_year DESC')
end

(This index page has Kaminari's pagination functionality)

Index view:

<% @paginate.each do |contest| %>
  <div class="submissionheading"><h2><%= contest.contest_year %>: <%= contest.organization.name %></h2></div>
  <% current_user.submissions.includes(:submission_details, :user, category: :award).order('created_at DESC').where(organization_id: contest.organization_id, contest_year: contest.contest_year).in_groups_of(3, false) do |group| %>
    <div class="row">
    <% group.each do |submission| %>
      <div class="col-md-4">
        <p class="bold"><%= submission.category.award.name %> <%= submission.category.code %>: <%= submission.category.name %></p>
        <p>Submitted By: <%= submission.user.first_name %> <%= submission.user.last_name %></p>
      </div>
    <% end %>
  </div><br/>
  <% end %>
<% end %>

<p><%= paginate @paginate %></p>

I get this error:

PG::Error: ERROR:  column "submissions.id" must appear in the GROUP BY clause or be used in an aggregate function

IF I insert the :id attribute into the group part of that above controller code, it repeats the submission information for every submission. Here's an illustration:

What it should be doing normally (without :id in the group part):

在此处输入图片说明

What it does once I insert :id in the group part:

在此处输入图片说明

It iterates for every submission, not just once for the three submissions that have the same org/year in common. Basically, I don't want to add :id to the grouping but PG is throwing this error! How can I re-write my controller code so PG accepts it while keeping the grouping correct? Thanks!

Either add select('DISTINCT(submissions.id), submissions.*') to the model finder or chain uniq onto it:

def index
  @paginate = current_user.submissions.uniq.group([:id, :organization_id, 
  :contest_year]).includes(:organization).page(params[:page]).per(3).order('contest_year DESC')
end

Update:

The problem is in your view code. You are iterating over the @paginate collection and then within it you are again calling current_user.submissions . Not sure what you're doing here but It's probably trippleing everything via the in_groups_of(3) call. Check that your @paginate collection is indeed unique and then debug your view code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM