A loop in a loop to fill an array?
-
21-08-2019 - |
Question
I am building a time registration program. Users can work on a project, and I want to display in a chart how many hours each user worked on a project, let's say, each month. The chart plugin works like this:
first_serie = OpenFlashChartLazy::Serie.new(
[["2008-1",100],["2008-2",120],["2008-3",130]],
{:title=>"name_of_user1",:start_date=>Time.mktime(2008,1,1),:items=>8})
This adds a new line in the graph.
My question is how can I loop through all my users and for each fill a new series with data from the database?
Solution
As a follow up to Pesto would be nicer to use inject.
@series = User.all.inject([]) do |mem, user|
mem << OpenFlashChartLazy::Serie.new(user.foo, user.bar, user.foobarbob)
end
Same code, just doesnt have a @series = []
OTHER TIPS
I have no idea how you generate all the data for Serie.new, but you can get started using this:
@series = []
users = User.find(:all)
users.each do |user|
@series << OpenFlashChartLazy::Serie.new(blah, blah, blah)
end
This will add all of the added Serie objects to an array.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow