From 9323d3bd0a7d7186cc378f87d93680cd34abdba9 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 16 Sep 2012 17:09:37 +0200 Subject: [PATCH] Fix query for wiki pages listing to work on all DBs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Wiki.group("slug").order("created_at")` in this case generates ambiguous group by query violating SQL standard: “The columns in a select list must be in the group by expression or they must be arguments of aggregate functions.” Therefore it doesn’t work on truly SQL compliant databases such as PostgreSQL. --- app/controllers/wikis_controller.rb | 2 +- app/models/wiki.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/controllers/wikis_controller.rb b/app/controllers/wikis_controller.rb index 55ccfe72116..c605a369293 100644 --- a/app/controllers/wikis_controller.rb +++ b/app/controllers/wikis_controller.rb @@ -7,7 +7,7 @@ class WikisController < ApplicationController layout "project" def pages - @wikis = @project.wikis.group(:slug).order("created_at") + @wikis = @project.wikis.last_revisions.order("created_at") end def show diff --git a/app/models/wiki.rb b/app/models/wiki.rb index ebb1ff49c7a..7b690f0abd1 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -28,6 +28,21 @@ class Wiki < ActiveRecord::Base end new_wiki end + + # Scope with last revisions of pages (i.e. newest page per project_id + # and slug). + # + # This is instead of Wiki.group(:slug).order(:created_at) which in this + # case generates query violating SQL standard (thus doesn't work on PgSQL). + # + # return: + # ActiveRecord::Relation + # + def last_revisions + t1 = arel_table.alias(table_name + '1') + where(created_at: from(t1).select(t1[:created_at].maximum) + .where(project_id: t1[:project_id], slug: t1[:slug])) + end end end # == Schema Information -- GitLab