'Add automatically new association to existing objects

I want to set-up a scorecard system base on criteria/sub-criteria pattern.

Example: https://www.datanovia.com/en/wp-content/uploads/2020/12/radar-chart-in-r-customized-fmstb-radar-chart-1.png

Given a Company(attrs: name) model, a Criteria(attrs: name) Model and a SubCriteria Model (attrs: name, score).

A Criteria has several SubCriterias,

For example: Advantage Criteria has SportClub subscription and Soccer ticket sub criterias.

I want for each existing companies in my DB (and future entries) to have automatically all these criterias and sub-criterias without duplicating everytime the sub-criteria.

I want at the end, to create a scorecard (like in the link au the beginning of the message) for every companies with score average on each criteria.

My question is : Is there any Rails relations to set-up to do it easily?

At the moment, i tried something like this:

  • Criteria has_many SubCriterias
  • SubCriteria belongs_to Criteria
  • Company has_many_through SubCriterias
  • SubCriteria has_many_through Companies

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source