与关联导入CSV

时间:2018-10-08 02:59:58

标签: ruby-on-rails csv roo

我有2个模型Order has_many line_itemsline_item belongs_to order。订单已创建,文件导入表单在订单显示表单上。我正在使用roo gem进行CSV导入,但是如果有其他解决方案,我也不必这样做。电子表格中的每一行都是一个新的line_item,但是我需要为每一行保存order_id。我愿意接受所有解决方案。

orders / show.html.erb

<%= form_tag import_line_items_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>

line_items_controller.rb

  def import
    LineItem.import(params[:file])
    redirect_to root_path, notice: "Products imported."
  end

  def edit
  end

  def create
    @line_item = LineItem.new(line_item_params, :order_id => :order_id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item, notice: 'Line item was successfully created.' }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

line_item.rb

class LineItem < ActiveRecord::Base
    require 'csv'
    belongs_to :order, inverse_of: :line_items

  def self.import(file)
    spreadsheet = Roo::Spreadsheet.open(file.path)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      line_item = find_by(id: row["id"]) || new
      line_item.attributes = row.to_hash
      line_item.order_id = @order
      puts @order
      line_item.save!
    end
  end  
end

这是我在使用此代码时遇到的错误...

Validation failed: Order must exist

1 个答案:

答案 0 :(得分:0)

您将必须将order传递给import操作和self.import方法。例如:

orders / show.html.erb

<%= form_tag import_line_items_path, multipart: true do %>
  # Send order_id via hidden_field_tag
  <%= hidden_field_tag :order_id, params[:id] %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>

line_items_controller.rb

def import
  order = Order.find(params[:order_id])
  LineItem.import(params[:file], order)
  redirect_to root_path, notice: "Products imported."
end

line_item.rb

def self.import(file, order)
  spreadsheet = Roo::Spreadsheet.open(file.path)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    line_item = find_by(id: row["id"]) || new
    line_item.attributes = row.to_hash
    line_item.order_id = order.id
    puts order
    line_item.save!
  end
end