diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 16887232c..5d2225a6e 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -16,7 +16,9 @@ class OrganisationsController < ApplicationController all_organisations = Organisation.order(:name) @pagy, @organisations = pagy(filtered_collection(all_organisations.visible, search_term)) - @merge_requests = MergeRequest.visible.order(:new_organisation_name) + @merge_requests = MergeRequest.visible + .joins("LEFT JOIN organisations ON organisations.id = merge_requests.absorbing_organisation_id") + .order("organisations.name") @searched = search_term.presence @total_count = all_organisations.visible.size end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 94b0aad3b..806c06623 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -19,7 +19,7 @@ class MergeRequest < ApplicationRecord scope :visible, lambda { open_collection_period_start_date = FormHandler.instance.start_date_of_earliest_open_collection_period where( - "(status != :merged_status) OR (status = :merged_status AND merge_date >= :open_collection_period_start_date)", + "(merge_requests.status != :merged_status) OR (merge_requests.status = :merged_status AND merge_requests.merge_date >= :open_collection_period_start_date)", merged_status: 4, open_collection_period_start_date:, ) @@ -31,4 +31,8 @@ class MergeRequest < ApplicationRecord errors.add(:new_organisation_name, :invalid) end end + + def absorbing_organisation_name + absorbing_organisation&.name || "" + end end diff --git a/app/views/organisations/_merge_request_list.html.erb b/app/views/organisations/_merge_request_list.html.erb index 1444a2e00..5926cfd2b 100644 --- a/app/views/organisations/_merge_request_list.html.erb +++ b/app/views/organisations/_merge_request_list.html.erb @@ -22,15 +22,14 @@ <% @merge_requests.each do |merge_request| %> <%= table.with_body do |body| %> <%= body.with_row do |row| %> - <% absorbing_organisation = @organisations.find { |org| org.id == merge_request.absorbing_organisation_id } %> - <% if absorbing_organisation&.name.blank? %> + <% if merge_request.absorbing_organisation_name.blank? %> <% row.with_cell(text: "You didn't answer this question", html_attributes: { scope: "row", class: "app-!-colour-muted", }) %> <% else %> - <% row.with_cell(text: absorbing_organisation&.name) %> + <% row.with_cell(text: merge_request.absorbing_organisation_name) %> <% end %> <% merge_date = merge_request.merge_date %> <% if merge_date.nil? %>