Files
nairobi/app/models/concerns/sdg/relatable.rb
Javi Martín 42699275a1 Add relations between relatable models and SDGs
Note we cannot directly get all related SDGs through SQL because they're
spread through different tables.
2020-12-04 18:27:49 +01:00

19 lines
463 B
Ruby

module SDG::Relatable
extend ActiveSupport::Concern
included do
has_many :sdg_relations, as: :relatable, dependent: :destroy, class_name: "SDG::Relation"
%w[SDG::Goal SDG::Target].each do |sdg_type|
has_many sdg_type.constantize.table_name.to_sym,
through: :sdg_relations,
source: :related_sdg,
source_type: sdg_type
end
end
def related_sdgs
sdg_relations.map(&:related_sdg)
end
end