大虾-table:遍历Active Record Relation Object来填充Content

Prawn-table: Iterating Through Active Record Relation Object to fill in Content

我正在使用 ruby gem: prawn and the extension prawn-table 来为大虾提供创建 table 的功能。

当你有静态数据时,用大虾 displayed here 创建 table 的基础很简单:

# This works.  Easy because it is static data
def prepare_for_print
  pdf = Prawn::Document.new
  pdf.font_size 11
  pdf.font "Times-Roman"

  pdf.table([ ["short", "short", "loooooooooooooooooooong "*30],
              ["short", "loooooooooooooooooooong "*15, "short"],
              ["loooooooooooooooooooong "*10, "short", "short"] ])
  return pdf
end

太棒了。那还不错。但是现在我想遍历一个活动记录关系对象,但我的尝试就是行不通:

def prepare_for_print
  pdf = Prawn::Document.new
  pdf.font_size 11
  pdf.font "Times-Roman"


  calls_by_disability.each do |disability|
    pdf.text "#{disability}", style: :bold, color: "001133"
    pdf.table([ ["Call ID", "Date", "County", "Service Category", "Service", "Notes"],
              disability.calls.each do |call|
                ["hello", "world", "foo", "bar", "bazz", "adsfsa"],
              end
              ])
  end
  return pdf
end

问题在于遍历关联调用:

disability.calls.each do |call|
  ["hello", "world", "foo", "bar", "bazz", "adsfsa"],
end

如有任何提示,我们将不胜感激。谢谢!

  calls_by_disability.each do |disability|
    pdf.text "#{disability}", style: :bold, color: "001133"

    header = ["Call ID", "Date", "County", "Service Category", "Service", "Notes"]
    table_data = []
    table_data << header
    disability.calls.map do |call|
      table_data << [call.id, call.date, call.country, call.service_category, call_service, call.notes]
    end
    pdf.table(table_data)
  end