【问题标题】:How do I create one CSV file containing two models with comma gem?如何使用逗号 gem 创建一个包含两个模型的 CSV 文件?
【发布时间】:2012-11-16 08:35:57
【问题描述】:

我想在我的 ruby​​ 3.2.8 应用程序中创建一个 CSV 文件,其中包含两个带有逗号 gem 的模型。也许这个问题的答案是微不足道的,但这是我第一次使用这个宝石。我知道如何根据模型创建文件,但我不知道如何匹配两个。

我有一个意见\参与者\索引:

<%= link_to 'Download CSV', '/participants.csv' %>

控制器:

def index
 @participants = Participant.all
 respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @participants }
   format.csv  { send_data @participants.to_comma }
 end
end

参与者模型:

require 'comma'
class Participant < ActiveRecord::Base
  comma do
    id
    token
  end
end

和字段模型:

require 'comma'
class Field < ActiveRecord::Base
  comma do
   name
   value
   id_participant
  end
end

在我拥有的数据库中:

Participant1 = ["id" => 1 , "token" => "a"]
Participant2 = ["id" => 2 , "token" => "b"] 
Field1= ["id_participant" => 1, "name" => "p1_exams1", "value" =>5]
Field2= ["id_participant" => 1, "name" => "p1_exams2", "value" =>3]
Field3= ["id_participant" => 2, "name" => "p2_exams1", "value" =>2]
Field4= ["id_participant" => 2, "name" => "p2_exams2", "value" =>3]

我想要一个这样的文件:

id   token
 1     a

id_p  name            value
 1   p1_c1_exams1      5
 1   p1_c1_exams2      3

id   token
 2     b

id_p  name            value
 2   p1_c1_exams1      2
 2   p1_c1_exams2      3    

我在控制器中尝试过:

def index
@participants = Participant.all
@fields = Field.all
require 'csv'
csv_string = CSV.generate do |csv|
    @participants.each do |p|
        csv << ["id","token","last_ip_address","start_date","last_transition_date","completion_date","completed","total_time_survey","created_at"]
        csv << [ p.id, p.token , p.last_ip_address, p.start_date, p.last_transition_date, p.completion_date, p.completed, p.total_time_survey, p.created_at]
        @fields.each do |f|
            if String(f.id_participant) == String(p.id)
                csv << ["id","name","value","id_participant","id_survey","created_at"]
                csv << [f.id,f.name, f.insert_value, f.id_participant, f.id_survey, f.created_at]
            end
        end
    end
end


respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @participants }
  format.csv  { send_data csv_string,
    :type => "text/csv; charset=iso-8859-1; header=present",
    :disposition => "attachment; filename=Database.csv" }
end
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 fastercsv


    【解决方案1】:

    您也可以为此使用 fastercsv 我认为这将帮助你我所理解的你在参与者和字段之间有很多关系我已经写了一些代码你可以根据你的需要自定义它

    @participants = Participant.all
      csv_string = FasterCSV.generate do |csv|
         @participants.each do |i|
         csv << ["id","token"]
         csv << [ i.id, i.token ]
         i.fields.each do |j|
          csv << ["id_p","name", "value"]
          csv << [i.id,j.name, j.value]
         end
         end
         end
     send_data csv_string,
              :type => "text/csv; charset=iso-8859-1; header=present",
              :disposition => "attachment; filename=anyName.csv"
    

    【讨论】:

    • 我现在试试,谢谢!但是,我要把这段代码放到控制器中吗?以及视图的调用,它是如何完成的?在模型中必须放一些说明?
    • 您可以将此添加到控制器。抱歉,我错过了在生成 csv 后发送数据的一些基本部分,现在我已经对我的解决方案进行了更改
    • ehm ,我有一个问题:请切换到 Ruby 1.9 的标准 CSV 库。它是 FasterCSV 加上对 Ruby 1.9 的 m17n 编码引擎的支持。但我有 1.9.3 Ruby 和 3.2.8 Rails...
    • 在 1.9 中,您不再需要/可以使用 fastcsv gem,因为它捆绑在 std 库中。现在您只需要从 Gem 文件中删除 gem 并在控制器中只需要“fastercsv”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2011-05-20
    相关资源
    最近更新 更多