I Ergebnisse meiner Abfrage Karte gruppiert nach organisation_id
wie so ein Array von Hashes zu erstellen.Schlüssel in Array von Hashes kombinieren
Das Ergebnis sieht so aus:
[{1=>{:name=>"cap1", :tags=>["tag A"], :scores=>{26=>4}}}, {1=>{:name=>"cap1", :tags=>["tag A"], :scores=>{12=>5}}}, {2 => {...}}...]
Für jeden organisation_id
es einen separaten Eintrag im Array ist. Ich möchte diese Hashes verschmelzen und verbinden die scores
Schlüssel wie so:
[{1=>{:name=>"cap1", :tags=>["tag A"], :scores=>{26=>4, 12=>5}}}, {2=>{...}}... ]
EDIT
Um die results
ich folgende AR verwenden zu erstellen:
Valuation.joins(:membership)
.where(capability: capability)
.select("valuations.id, valuations.score, valuations.capability_id, valuations.membership_id, memberships.location_id, memberships.organisation_id")
.map(&:serializable_hash)
Ein Valuation
Modell:
class Valuation < ApplicationRecord
belongs_to :membership
belongs_to :capability
end
A Membership
Modell:
class Membership < ApplicationRecord
belongs_to :organisation
belongs_to :location
has_many :valuations
end
results
Schnipsel:
[{"id"=>1, "score"=>4, "capability_id"=>1, "membership_id"=>1, "location_id"=>26, "organisation_id"=>1}, {"id"=>16, "score"=>3, "capability_id"=>1, "membership_id"=>2, "location_id"=>36, "organisation_id"=>1}, {"id"=>31, "score"=>3, "capability_id"=>1, "membership_id"=>3, "location_id"=>26, "organisation_id"=>2}, {"id"=>46, "score"=>6, "capability_id"=>1, "membership_id"=>4, "location_id"=>16, "organisation_id"=>2}...
Warum die Eile, um eine Antwort auszuwählen? –
Ihre Frage würde stark verbessert, indem Sie ein Beispiel für "Ergebnisse" geben. –
Cary, siehe letzte Änderung –