我有两个模型:<代码>项目和 www.un.org/spanish/ecosoc (言辞实际上只是一个模式,每个项目每天写上字数。)
我认为,我也这样做,这表明该项目从一开始至尾都有几天时间,以及当天写过多少字。
<% project_range(@project.start, @project.end).each do |day| %>
<%= day %>
<%= get_word_count_by_date(@project, day ) %>
<% end %>
我的帮助者:
def project_range(start, finish)
project_days = (start..finish).collect
end
def get_word_count_by_date(project, date)
word_count = Word.find_by_project_id_and_wrote_on(project, date)
if word_count
word_count.quantity
else
0
end
end
据认为,这可打到我的数据库。 例如,如果该项目为30天,我可以:
Word Load (0.2ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-01 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-02 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-03 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-04 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-05 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-06 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-07 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-08 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-09 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-10 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-11 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-12 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-13 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-14 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-15 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-16 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-17 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-18 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-19 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-20 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-21 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-22 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-23 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-24 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-25 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-26 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-27 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-28 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-29 LIMIT 1
Word Load (0.1ms) SELECT "words".* FROM "words" WHERE "words"."project_id" = 2 AND "words"."wrote_on" = 2011-09-30 LIMIT 1
如果没有在项目时间上每一天提出询问,是否有办法这样做? 首先,我试图把项目的所有字装上一字,但无法说明如何用零用一天。