Rails CSV IMPORT first_or_create - PullRequest
       67

Rails CSV IMPORT first_or_create

3 голосов
/ 17 октября 2019

Привет У меня есть очень простой импортер CSV, который пользователь использует для импорта товаров. Предметы принадлежат: номер:

Когда пользователь импортирует элементы, которые я хочу добавить в первую очередь, или создать в импорте, чтобы найти или создать номер детали по его имени. Столбцы CSV-файла, я хочу иметь

name, part_number.name

Схема

 create_table "items", force: :cascade do |t|
        t.bigint "project_id"
        t.string "name"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.integer "status", default: 0
        t.bigint "part_number_id"
        t.index ["part_number_id"], name: "index_items_on_part_number_id"
        t.index ["project_id"], name: "index_items_on_project_id"
      end

create_table "part_numbers", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

app / models / item.rb

    class Item < ApplicationRecord
          belongs_to :project
          belongs_to :part_number

          def self.import(file)
            CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
              Item.create! row.to_hash
            end
          end
end

app / models / part_number.rb

class PartNumber < ApplicationRecord
    has_many :items
end

app / controllers / projects / items_controller.rb

класс Projects :: ItemsController

  # GET /items/new
  def new
    @project = Project.find(params[:project_id])
    @item = Item.new
  end

  def index
    @project = Project.find(params[:project_id])
    @items = @project.items.all
    respond_to do |format|
      format.html
      format.csv { send_data @items.to_csv }
    end
  end
  # GET /items/1/edit
  def edit
  end

  # POST /items
  # POST /items.json
  def create
    @project = Project.find(params[:project_id])
    @item = Item.new(item_params)
    @item.project_id = @project.id
    respond_to do |format|
      if @item.save
        format.html { redirect_to @item.project, notice: 'Item was successfully created.' }
        format.json { render :show, status: :created, location: @item.project }
      else
        format.html { render :new }
        format.json { render json: @item.project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /items/1
  # PATCH/PUT /items/1.json
  def update
    @item = Item.find(params[:id])
    @project = Project.find(params[:project_id])
    respond_to do |format|
      if @item.update(item_params)
        format.html { redirect_to @item.project, notice: 'Item was successfully updated.' }
        format.json { render :show, status: :ok, location: @item.project }
      else
        format.html { render :edit }
        format.json { render json: @item.project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /items/1
  # DELETE /items/1.json
  def destroy
    @item = Item.find(params[:id])
    @project = Project.find(params[:project_id])
    title = @item.model
    if @item.destroy
      flash[:notice] = "One \'#{title}' was successfully destroyed."
      redirect_to @project 
    else
      flash[:notice] = "Error Yo"
      render :show
    end
  end

  def import
    @project = Project.find(params[:project_id])
    @project.items.import(params[:file])
    redirect_to projects_path(@project), notice: "Sucessfully Imported Items!"
   end

  private
    # Use callbacks to share common setup or constraints between actions.

    # Never trust parameters from the scary internet, only allow the white list through.
    def item_params
      params.require(:item).permit(:model, :project_id, :name, :search, part_number: [:id, :name])
    end
end

1 Ответ

1 голос
/ 17 октября 2019

Если в качестве первого столбца в каждой строке (строка [0]) указывается имя вашего параметра, то, я думаю, что-то вроде этого должно работать:

class Item < ApplicationRecord
  belongs_to :project
  belongs_to :part_number

  def self.import(file)
    CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
      Item.where(name: row[0]).find_or_create_by do |item|
        item.update_attributes(row.to_hash)
      end
    end
  end
end

Достойное руководство по использованию find_or_create_by при импортеCSV:

https://www.driftingruby.com/episodes/importing-and-exporting-csv-data

...