Ich habe folgende ModelleRails/SQL: Finden Sie Statistiken über eine belongs_to Beziehung
class Project
has_many :contributions
end
class Contributor
has_many :contributions
end
class Contribution
belongs_to :contributor
belongs_to :project
end
Ich versuche, wie viele Autoren in wie viele Projekte trugen zu finden und sortieren nach Anzahl der Projekte beigetragen.
Beispiel:
- Person 1 made contribution to Project 1
- Person 1 made contribution to Project 2
- Person 2 made contribution to Project 1
- Person 2 made contribution to Project 3
- Person 2 made contribution to Project 4
- Person 3 made contribution to Project 4
- Person 3 made contribution to Project 5
In diesem Fall
- Person 1 made 2 contributions in 2 seperate projects.
- Person 2 made 3 contributions in 3 seperate projects.
- Person 3 made 2 contributions in 2 seperate projects.
was bedeutet
- 2 people made 2 contributions
- 1 person made 3 contributions
Ergebnis ist: { 2 => 2, 1 => 3 }
Hier ist, was ich getan habe:
Dies gibt mir, wie viele Beiträge von jedem Mitarbeiter, aber nicht das, was ich suche.
Ist das für Sie nützlich? –