Files
nairobi/app/models/sdg/goal.rb
Javi Martín 96d5354cd8 Improve performance sorting SDG records
We noticed there was a performance issue while browsing the SDG
Management section and when one of our tests started failing sometimes
because the request to the relations#index controller took too long.

The issue proved to be `SDG::Target#<=>`. This method calls `.goal` for
each target, meaning we were generating 169 database queries when
sorting all targets.

So we're comparing codes directly to minimize the number of database
queries and improve performance. Requests to the relations index take
now less than third of the time they used to take.
2021-01-30 14:00:44 +01:00

29 lines
559 B
Ruby

class SDG::Goal < ApplicationRecord
include SDG::Related
validates :code, presence: true, uniqueness: true, inclusion: { in: 1..17 }
has_many :targets, dependent: :destroy
has_many :local_targets, dependent: :destroy
def title
I18n.t("sdg.goals.goal_#{code}.title")
end
def title_in_two_lines
I18n.t("sdg.goals.goal_#{code}.title_in_two_lines")
end
def description
I18n.t("sdg.goals.goal_#{code}.description")
end
def self.[](code)
find_by!(code: code)
end
def code_and_title
"#{code}. #{title}"
end
end