循环中的循环来填充数组?

时间:2022-11-29 16:05:09

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?

我的问题是我如何遍历所有用户并为每个用户填充包含数据库数据的新系列?

2 个解决方案

#1


As a follow up to Pesto would be nicer to use inject.

作为Pesto的后续使用注射会更好。

@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 = []

相同的代码,只是没有@series = []

#2


I have no idea how you generate all the data for Serie.new, but you can get started using this:

我不知道你如何为Serie.new生成所有数据,但是你可以开始使用它:

@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.

这会将所有添加的Serie对象添加到数组中。

#1


As a follow up to Pesto would be nicer to use inject.

作为Pesto的后续使用注射会更好。

@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 = []

相同的代码,只是没有@series = []

#2


I have no idea how you generate all the data for Serie.new, but you can get started using this:

我不知道你如何为Serie.new生成所有数据,但是你可以开始使用它:

@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.

这会将所有添加的Serie对象添加到数组中。