Rails find_or_initialize_by 仅不插入数据 "created_at", "updated_at"
Rails find_or_initialize_by not inserting data only "created_at", "updated_at"
我正在尝试从 csv 中提取数据并使用 rake 插入到数据库中。由于某种原因,插入语句不包括提供的数据。正如您在 Development.log 文件中看到的那样,它会搜索不存在的正确记录,然后尝试创建该记录。但只为 "created_at"、"updated_at".
插入日期戳
我是 rails 的新手,所以这可能很简单,但我找不到答案 online.I 一直在试图弄清楚为什么它一整天都不工作,任何帮助非常感谢
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 控制台 > 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 输出符号,从而增加了混乱。数据一直到活动记录,但随后这些属性从插入语句中删除。
已解决:
根据 Jordan Dedels 的建议,问题出在我的 Plant.rb 模型中的 attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name
。标记为已回答。
attr_accessor
应该只用于您 不想 存储在数据库中的属性。删除这一行,它应该可以工作:
attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name
我正在尝试从 csv 中提取数据并使用 rake 插入到数据库中。由于某种原因,插入语句不包括提供的数据。正如您在 Development.log 文件中看到的那样,它会搜索不存在的正确记录,然后尝试创建该记录。但只为 "created_at"、"updated_at".
插入日期戳我是 rails 的新手,所以这可能很简单,但我找不到答案 online.I 一直在试图弄清楚为什么它一整天都不工作,任何帮助非常感谢
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 控制台 > 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 输出符号,从而增加了混乱。数据一直到活动记录,但随后这些属性从插入语句中删除。
已解决:
根据 Jordan Dedels 的建议,问题出在我的 Plant.rb 模型中的 attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name
。标记为已回答。
attr_accessor
应该只用于您 不想 存储在数据库中的属性。删除这一行,它应该可以工作:
attr_accessor :Accepted_Symbol, :Scientific_Name, :Common_Name