rake import导致没有模型ID

时间:2016-09-23 14:57:27

标签: ruby-on-rails ruby postgresql

我有一个使用psql的rails应用程序,并通过bundle exec rake import函数将csv文件导入rails。但是,当我这样做时,我注意到我的模型没有任何与它们相关联的ID。我的模型名为Five,当我在导入后进入rails控制台检查id时,我得到id = nil(参见下面的代码)。 rake导入正在加载数据集(我通过控制台中的Five.count进行了检查)。知道模型为什么不分配ID?我需要id在app中的其他命令中调用它们。

控制台ID检查

2.2.1 :011 > Five.count
   (1.8ms)  SELECT COUNT(*) FROM "fives"
 => 100 
2.2.1 :012 > Five.first.valid?
  Five Load (4.3ms)  SELECT  "fives".* FROM "fives" LIMIT 1
 => true
2.2.1 :003 > Five.first.id
  Five Load (5.6ms)  SELECT  "fives".* FROM "fives" LIMIT 1
 => nil 

Rake导入代码,位于lib / tasks / import.rake

desc "Imports a CSV file into an ActiveRecord table"
task :import, [:filename] => :environment do    
    CSV.foreach('/Users/UserName/desktop/test55.csv', :headers => true) do |row|
      Five.create! row.to_hash
    end
end

五个控制器

class FivesController < ApplicationController

  def index
    @fives = Five.paginate(:per_page => 12, :page => params[:page])

  end

   def show
     @user = current_user 
     @five = Five.find(params[:id])
  end


  def import
     Five.import(params[:file])
     redirect_to fives_path, notice: "Fives imported."
  end

    def create
    @five = Five.new(five_params)

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

  # GET /users/new
  def new
    @five = Five.new
  end

    def five_params
      params.require(:five).permit(:SPONSOR_DFE_NAME)
    end

end

DB Schmea

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "fives", id: false, force: :cascade do |t|
    t.text    "row.names"
    t.boolean "SPONS_DFE_EIN"
    t.text    "Filing_Date"
    t.text    "Filing_Type"
    t.text    "Filing_Method"
    t.text    "Filing_Name"
    t.text    "Plan_Name"
    .
    .
    .

0 个答案:

没有答案