Reputation: 47
I have a model Defect with attribute found_in. I have a hash test_phases whose keys are various test phases and whose value is an array of found_in values. Is there a way to group the Defect by test_phases? something like Defect.group_by(?test_phases)?.
The code I'm using is ugly
defects = {}
test_phases.each do |test_phase, found_ins|
defects[test_phase] ||= Defect.where(found_in: found_ins].all
end
Upvotes: 0
Views: 658
Reputation: 47
I solved this by creating a TestPhase model with a join table DefectFound
test_phase.rb:
has_many :defect_founds
defect_found.rb:
belongs_to :test_phase
defect.rb:
belongs_to :defect_found, foreign_key: :found_in, primary_key: :name # defect_found.name = defect.found_in
has_one :test_phase, through: :defect_found
controller:
@defects = Defect.includes(:test_phase).select('found_in, COUNT(id) as backlog').group(:found_in).group_by(&:test_phase)
view:
%table
- @defects.each do |test_phase, backlogs|
%tr
%td= test_phase.name if test_phase.present?
%td= backlogs.map(&:backlog).inject(:+)
Upvotes: 0
Reputation: 67910
You don't need to group, since you are iterating a hash (no duplicate keys), the output hash won't have multiple elements by key. Simply use map
+ Hash
(or mash for Facets connoisseurs):
defects = Hash[test_phases.map do |test_phase, found_in_values|
[test_phase, Defect.where(found_in: found_in_values)]
end]
Upvotes: 1