From 126286987ce51dbdea5ae3765c9a9a72d027d729 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Sun, 12 Jan 2025 23:32:56 -0700 Subject: [PATCH 1/2] Fix `Template.latest_version` ambiguity error - Prior to this commit, the following error was encountered when applying the search filter within `/org_admin/templates`: ``` ActiveRecord::StatementInvalid - PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous LINE 1: SELECT COUNT(DISTINCT id) FROM (SELECT MAX(version) AS versi... ^: app/views/layouts/_paginable.html.erb:5 app/controllers/concerns/paginable.rb:82:in `paginable_renderise' app/controllers/paginable/templates_controller.rb:25:in `index' ``` - Tracing the code reveals that the query encountering this error is `Template.includes(:org).latest_version.where(customization_of: nil).distinct.total_count`. - Although `SELECT "templates.*"` is the default behaviour of the `.latest_version` scope, the ambiguity error is raised unless `.select('templates.*')` is added explicitly. --- app/models/template.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/template.rb b/app/models/template.rb index 4e83978b6e..c698367ad5 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -136,6 +136,10 @@ class Template < ApplicationRecord AND current.family_id = templates.family_id INNER JOIN orgs ON orgs.id = templates.org_id SQL + # `SELECT "templates.*"` is this scope's default behavior, but it must be explicitly + # specified to prevent `PG::AmbiguousColumn` errors when `.distinct.total_count` is used + # (e.g. in `app/views/layouts/_paginable.html.erb`) + .select('templates.*') } # Retrieves the latest customized versions, i.e. those with maximum version From 3125bc9a1e1a476a7b6fc87a766ad20c7d7ae876 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Mon, 13 Jan 2025 11:33:08 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c1959f48f..1c1fd4688d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ - Refactor Plan.deep_copy(plan) [#3469](https://github.com/DMPRoadmap/roadmap/pull/3469) - Fixed a bug in the deep copy of plans where the old identifier was being copied into the new plan. We now copy the generated id of the new plan to the identifier field. - Fixed bar chart click function in the Usage dashboard (GitHub issue #3443) - +- Fix `Template.latest_version` Ambiguity Error [#3474](https://github.com/DMPRoadmap/roadmap/pull/3474) **Note this upgrade is mainly a migration from Bootstrap 3 to Bootstrap 5.**