ruby 虾桌:遍历活动记录关系对象填写内容

hts6caw3  于 2023-05-17  发布在  Ruby
关注(0)|答案(1)|浏览(76)

使用Ruby Gem:prawn和扩展prawn-table来为prawn提供创建表格的功能。
使用prawn displayed here创建一个表的基础知识对于静态数据来说很简单:

# 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

任何提示是赞赏。谢谢!

dpiehjr4

dpiehjr41#

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

相关问题