I'm running into several syntax and nil issues when trying to create this hash idea_benefit_count.
@idea = @domain.ideas
@idea_evaluations = Array.new()
@idea.each do |idea|
@idea_evaluations << idea.evaluations
end
@idea_benefit = []
if !@idea_evaluations.nil?
@idea_evaluations.each do |eval|
@idea_benefit << eval.benefit
end
@idea_benefit_count = Hash.new(0)
@idea_benefit.flatten.each { |idea_benefit| @idea_benefit_count[idea_benefit] += 1 }
end
An idea has_many domains, so @idea should be all of the ideas that has a given @domain.
@idea_evaluations is an array of all evaluations that belong_to a specific idea.
@idea_benefit is an array of arrays that holds all of each evaluation's benefit entries. example output: [["happier_customers"], ["happier_employees"], ["happier_employees", "decreased_costs"], ["happier_employees"]]
idea_benefit_count reads idea_benefit and counts how many of each type of benefit an idea has. The output of idea_benefit_count based on the above idea_benefit array is {"happier_customers"=>1, "decreased_costs"=>1, "happier_employees"=>3}
(Hopefully this context covers everything)
My current error is undefined method 'benefit' on the line @idea_benefit << eval.benefit. Benefit is an attribute of evaluation, so it should output something like ["happier_customers"].
I realize this code is a bit of a mess, so any suggestions would be super helpful on getting the entire thing working. A big problem is checking to see if there are evaluations belonging to an idea, because if this comes back nil it throws an error. Tried to fix that with the if !@idea_evaluations.nil? check, but haven't been able to test it yet because of the undefined method 'benefit' error.
EDIT
@idea_evaluations = Array.new()
@idea.each do |idea|
@idea_evaluations << idea.evaluations
end
@idea_evaluations.flatten!
@idea_benefit = []
unless !@idea_evaluations.nil?
@idea_evaluations.each do |eval|
@idea_benefit << eval.benefit
end
@idea_benefit_count = Hash.new(0)
@idea_benefit.flatten.each { |idea_benefit| @idea_benefit_count[idea_benefit] += 1 }
end