Rails find_or_initialize_by不仅仅插入数据" created_at"," updated_at"

时间:2015-02-19 23:36:10

标签: ruby-on-rails ruby csv activerecord

我正在尝试从csv中提取数据并使用rake插入数据库。由于某种原因,insert语句不包括提供的数据。正如您在Development.log文件中所看到的,它会搜索不存在的正确记录,然后尝试创建记录。但只插入" created_at"," updated_at"的日期戳。

我是rails的新手,所以它可能很简单,但我无法在网上找到答案。我一直在试图弄清楚为什么它不能全天工作,非常感谢任何帮助

USDAData.csv

"ABELI","Abelia","abelia"
"ABGR4","Abelia ×grandiflora","glossy abelia"
"ABELM","Abelmoschus","okra"
"ABES","Abelmoschus esculentus","okra"
"ABMA9","Abelmoschus manihot",""

USDAData.rake

namespace :USDAData do
  desc "import data from USDA to database"
    task :import => :environment do
    file = File.open(File.join(Rails.root, 'db', 'USDAData.csv'))

    file.each do |line|
        attrs = line.split(",")
        @Accepted_Symbol = attrs[0].gsub!(/\A"|"\Z/, '')
        @Scientific_Name = attrs[1].gsub!(/\A"|"\Z/, '').titleize
        @Common_Name = attrs[2].gsub!(/\A"|"\Z/, '').titleize

        p = Plant.find_or_create_by(Accepted_Symbol: @Accepted_Symbol, Scientific_Name: @Scientific_Name, Common_Name: @Common_Name)

    end
  end
end

development.log

  [1m[36mPlant Load (0.4ms)[0m  [1mSELECT  "plants".* FROM "plants" WHERE "plants"."Accepted_Symbol" = ? AND "plants"."Scientific_Name" = ? AND "plants"."Common_Name" = ? LIMIT 1[0m  [["Accepted_Symbol", "ABELI"], ["Scientific_Name", "Abelia"], ["Common_Name", "Abelia\n"]]
  [1m[35m (0.2ms)[0m  begin transaction
  [1m[36mSQL (0.5ms)[0m  [1mINSERT INTO "plants" ("created_at", "updated_at") VALUES (?, ?)[0m  [["created_at", "2015-02-19 23:20:54.468007"], ["updated_at", "2015-02-19 23:20:54.468007"]]
  [1m[35m (9.6ms)[0m  commit transaction
  [1m[36mPlant Load (0.2ms)[0m  [1mSELECT  "plants".* FROM "plants" WHERE "plants"."Accepted_Symbol" = ? AND "plants"."Scientific_Name" = ? AND "plants"."Common_Name" = ? LIMIT 1[0m  [["Accepted_Symbol", "ABGR4"], ["Scientific_Name", "Abelia ×Grandiflora"], ["Common_Name", "Glossy Abelia\n"]]
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mSQL (0.3ms)[0m  [1mINSERT INTO "plants" ("created_at", "updated_at") VALUES (?, ?)[0m  [["created_at", "2015-02-19 23:20:54.482516"], ["updated_at", "2015-02-19 23:20:54.482516"]]
  [1m[35m (9.8ms)[0m  commit transaction
  [1m[36mPlant Load (0.1ms)[0m  [1mSELECT  "plants".* FROM "plants" WHERE "plants"."Accepted_Symbol" = ? AND "plants"."Scientific_Name" = ? AND "plants"."Common_Name" = ? LIMIT 1[0m  [["Accepted_Symbol", "ABELM"], ["Scientific_Name", "Abelmoschus"], ["Common_Name", "Okra\n"]]

plant.rb

class Plant < ActiveRecord::Base
  attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name
  validates :Accepted_Symbol, presence: true, :null => false
  validates :Scientific_Name, presence: true, :null => false
  validates :Common_Name, presence: true

  before_save :test

  def test
    puts "test: #{@Accepted_Symbol}"
  end

end

schema.rb

ActiveRecord::Schema.define(version: 20150218210119) do

  create_table "plants", force: :cascade do |t|
    t.string   "Accepted_Symbol"
    t.string   "Scientific_Name"
    t.string   "Common_Name"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

end

Rails控制台&gt; Plant.first(5)

>> Plant.first(5)
  Plant Load (0.3ms)  SELECT  "plants".* FROM "plants"  ORDER BY "plants"."id" ASC LIMIT 5
=> [#<Plant id: 1, Accepted_Symbol: nil, Scientific_Name: nil, Common_Name: nil, created_at: "2015-02-19 23:20:54", updated_at: "2015-02-19 23:20:54">, 
#<Plant id: 2, Accepted_Symbol: nil, Scientific_Name: nil, Common_Name: nil, created_at: "2015-02-19 23:20:54", updated_at: "2015-02-19 23:20:54">,
#<Plant id: 3, Accepted_Symbol: nil, Scientific_Name: nil, Common_Name: nil, created_at: "2015-02-19 23:20:54", updated_at: "2015-02-19 23:20:54">, 
#<Plant id: 4, Accepted_Symbol: nil, Scientific_Name: nil, Common_Name: nil, created_at: "2015-02-19 23:20:54", updated_at: "2015-02-19 23:20:54">, 
#<Plant id: 5, Accepted_Symbol: nil, Scientific_Name: nil, Common_Name: nil, created_at: "2015-02-19 23:20:54", updated_at: "2015-02-19 23:20:54">]

before_save输出符号,从而增加了混乱。数据一直到活动记录,但然后从insert语句中删除这些属性。

解决: 正如Jordan Dedels所建议的,我的Plant.rb模型中的问题是attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name。标记为已回答。

1 个答案:

答案 0 :(得分:1)

attr_accessor只应用于要存储在数据库中的属性。删除此行,它应该工作:

attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name