diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index e4997e0e5..43987eee4 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -33,7 +33,12 @@ def index end format.json do # For the widget and treeview response = if params[:root].present? - collections = Iqvoc::Collection.base_class.with_pref_labels.published.by_parent_id(params[:root]) + collections = Iqvoc::Collection.base_class + .with_pref_labels + .published + .by_parent_id(params[:root]) + .sort_by { |c| c.pref_label.to_s } + collections.map do |collection| res = { id: collection.id, @@ -46,10 +51,12 @@ def index res end else - collections = Iqvoc::Collection.base_class.with_pref_labels.published.merge(Label::Base.by_query_value("#{params[:query]}%")) - collections.map do |c| - collection_widget_data(c) - end + collections = Iqvoc::Collection.base_class + .with_pref_labels + .published + .merge(Label::Base.by_query_value("#{params[:query]}%")) + .sort_by { |c| c.pref_label.to_s } + .map { |c| collection_widget_data(c) } end render json: response end diff --git a/app/models/collection/base.rb b/app/models/collection/base.rb index b281f8c37..4cfa1c875 100644 --- a/app/models/collection/base.rb +++ b/app/models/collection/base.rb @@ -68,7 +68,10 @@ class Collection::Base < Concept::Base validate :circular_subcollections def self.with_pref_labels - super.references(:collection_members) + preload(:pref_labels) + .joins(:pref_labels) + .references(:collection_members) + .distinct end def self.by_origin(origin)