diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index beab8fa0..9abd8f6f 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -28,7 +28,7 @@ class ErrorsController < ApplicationController def index authorize! - @errors = Error.for_execution_environment(@execution_environment) + @errors = Error.for_execution_environment(@execution_environment).grouped_by_message end def set_execution_environment diff --git a/app/models/error.rb b/app/models/error.rb index 89b83962..d52b3a34 100644 --- a/app/models/error.rb +++ b/app/models/error.rb @@ -1,9 +1,8 @@ class Error < ActiveRecord::Base belongs_to :execution_environment - scope :for_execution_environment, ->(execution_environment) do - Error.find_by_sql("SELECT MAX(created_at) AS created_at, MAX(id) AS id, message, COUNT(*) AS count FROM errors WHERE #{sanitize_sql_hash_for_conditions(execution_environment_id: execution_environment.id)} GROUP BY message ORDER BY count DESC") - end + scope :for_execution_environment, ->(execution_environment) { where(execution_environment_id: execution_environment.id) } + scope :grouped_by_message, -> { select('MAX(created_at) AS created_at, MAX(id) AS id, message, COUNT(id) AS count').group(:message).order('count DESC') } validates :execution_environment_id, presence: true validates :message, presence: true diff --git a/spec/controllers/errors_controller_spec.rb b/spec/controllers/errors_controller_spec.rb index 4da324dc..f402e4c7 100644 --- a/spec/controllers/errors_controller_spec.rb +++ b/spec/controllers/errors_controller_spec.rb @@ -67,7 +67,7 @@ describe ErrorsController do expect_assigns(execution_environment: :execution_environment) it 'aggregates errors by message' do - expect(assigns(:errors).count).to eq(1) + expect(assigns(:errors).length).to eq(1) end expect_status(200)