From 28ae40792139025270ad3b79bae6b7db0723c749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Bol=C3=ADvar?= Date: Wed, 16 Aug 2023 09:20:53 +0200 Subject: [PATCH] Fix ambiguous id column on projects query (#11474) (#11482) --- .../app/models/decidim/budgets/project.rb | 2 +- .../models/decidim/budgets/project_spec.rb | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/decidim-budgets/app/models/decidim/budgets/project.rb b/decidim-budgets/app/models/decidim/budgets/project.rb index ea98312c4d6c2..d2909ab59ba82 100644 --- a/decidim-budgets/app/models/decidim/budgets/project.rb +++ b/decidim-budgets/app/models/decidim/budgets/project.rb @@ -52,7 +52,7 @@ def self.ordered_ids(ids) # array. This is why we search for match ",2," instead to get the actual # position for ID 2. concat_ids = connection.quote(",#{ids.join(",")},") - order(Arel.sql("position(concat(',', id::text, ',') in #{concat_ids})")) + order(Arel.sql("position(concat(',', decidim_budgets_projects.id::text, ',') in #{concat_ids})")) end def self.log_presenter_class_for(_log) diff --git a/decidim-budgets/spec/models/decidim/budgets/project_spec.rb b/decidim-budgets/spec/models/decidim/budgets/project_spec.rb index e180afef0c92f..0950f81178dbd 100644 --- a/decidim-budgets/spec/models/decidim/budgets/project_spec.rb +++ b/decidim-budgets/spec/models/decidim/budgets/project_spec.rb @@ -36,7 +36,18 @@ module Decidim::Budgets describe ".ordered_ids" do let(:budget) { create(:budget, total_budget: 1_000_000) } - let(:projects) { create_list(:project, 50, budget: budget, budget_amount: 100_000) } + let(:category) { create(:category, participatory_space: budget.participatory_space) } + let(:projects) { create_list(:project, 50, budget: budget, budget_amount: 100_000, category: category) } + let(:test_ids) do + first = described_class.where(budget: budget).order(:id).pluck(:id)[0..3] + ids = described_class.where(budget: budget).pluck(:id).shuffle + + # Put the first items at the end of the IDs array in order to get + # possibly "conflicting" matches for them at earlier array positions. + # As we have 50 projects, we should have IDs starting with 1, 2, 3 and 4 + # which is why we put the first 4 items at the end. + (ids - first) + first + end before do # Reset the project IDs to start from 1 in order to get possibly @@ -50,17 +61,12 @@ module Decidim::Budgets end it "returns the correctly ordered projects" do - first = described_class.where(budget: budget).order(:id).pluck(:id)[0..3] - ids = described_class.where(budget: budget).pluck(:id).shuffle - - # Put the first items at the end of the IDs array in order to get - # possibly "conflicting" matches for them at earlier array positions. - # As we have 50 projects, we should have IDs starting with 1, 2, 3 and 4 - # which is why we put the first 4 items at the end. - test_ids = (ids - first) + first - expect(described_class.ordered_ids(test_ids).pluck(:id)).to eq(test_ids) end + + it "returns the correctly ordered projects after filtering by category" do + expect(described_class.with_any_category([category.id]).ordered_ids(test_ids).pluck(:id)).to eq(test_ids) + end end describe "#orders_count" do