Skip to content

Commit

Permalink
Fix ambiguous id column on projects query (decidim#11474) (decidim#11482
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fblupi authored Aug 16, 2023
1 parent d2f2766 commit 28ae407
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion decidim-budgets/app/models/decidim/budgets/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 16 additions & 10 deletions decidim-budgets/spec/models/decidim/budgets/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 28ae407

Please sign in to comment.