尝试播种CSV数据时为什么会看到此错误消息

时间:2020-03-23 17:59:51

标签: ruby-on-rails

尝试播种csv数据时在我的终端中获得此错误消息:

耙子中止了! ActiveRecord :: AssociationTypeMismatch:预计年份(#70340979867760),获得了1967,它是Integer(#70340969823320)的实例

这是我的rake文件代码:

require 'csv'
namespace :albums do
  desc "pull albums data into database"
  task seed_albums: :environment do

    #drop the old table data before importing the new stuff
    Albums.destroy_all

    CSV.foreach("lib/assets/Albums.csv", :headers =>true) do |row |
      puts row.inspect #just so that we know the file's being read

      #create new model instances with the data
      Albums.create!(
      name: row[0],
      year: row[1].to_i)
    end
  end
end

这是我的计划代码:

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

这是我的控制器代码:

class AlbumsController < ApplicationController
  before_action :set_albums, only: [:show, :edit, :update, :destroy]

  # GET /albums
  # GET /albums.json
  def index
    @albums = Albums.all
  end


  # GET /albums/1
  # GET /albums/1.json
  def show
  end

  # GET /albums/new
  def new
    @albums = Albums.new (params[:album])
  end

  # GET /albums/1/edit
  def edit
  end

  # POST /albums
  # POST /albums.json
  def create
    @albums = Albums.new

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

  # PATCH/PUT /albums/1
  # PATCH/PUT /albums/1.json
  def update
    respond_to do |format|
      if @albums.update(albums_params)
        format.html { redirect_to @albums, notice: 'albums was successfully updated.' }
        format.json { render :show, status: :ok, location: @albums }
      else
        format.html { render :edit }
        format.json { render json: @albums.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /albums/1
  # DELETE /albums/1.json
  def destroy
    @albums.destroy
    respond_to do |format|
      format.html { redirect_to albums_url, notice: 'albums was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_albums
      @albums = albums.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def albums_params
      params.require(:albums).permit(:name, :term)
    end
end

预先感谢

0 个答案:

没有答案