自定义 Rake 种子文件(Rails 4.2.1 Ruby 2.2.3) - 多次循环并在数据库中创建重复条目



我已经设置了一个自定义的耙子种子任务来填充我的数据库。我的 rake 任务设置为循环遍历数组一次(在执行时)并用所述数据填充数据库。出于某种原因,当我运行一次任务时,耙子任务似乎运行了五次。

此外,循环通过的每个仓位都会在数据库中创建一个额外的 nil 值记录。

在盯着我的代码之后,我无法推理这两个问题正在发生。任何帮助都非常感谢!

**testdata.rb**
questions = [
   ["What is the supreme law of the land?n1) the Ley of the Landn2) the Article of Lawn3) the Constitutionn4) Article One of the Constitution", "3", "The Constitution is the "supreme law of the land" and establishes the basic principles of the US govt. It lists fundamental rights for people living in the USA.", "Version1", "English"],
   ["The "supreme law of the land" is not the Constitution.n1) Truen2) False", "2", "The Constitution is the "supreme law of the land" and establishes the basic principles of the US govt. It lists fundamental rights for people living in the USA.", "Version1", "English"]
]
questions.each do | body, correct_answer, explanation, version, language |
   Question.create(subject_id: 3, body: body, correct_answer: correct_answer, explanation: explanation, version: version, language: language, active: true)
end

**test.rake**
namespace :db do
  namespace :seed do
    Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |testdata|
      task_name = File.basename(testdata, '.rb').intern    
      desc "Load the seed data from db/seeds/#{task_name}.rb"
      task task_name => :environment do
        load(testdata) if File.exist?(testdata)
      end
    end
  end
end

**Output in SQLLITE ActiveRecord query after running rake task**
MacbookAir $rake db:seed:testdata
MacbookAir $rails c
Running via Spring preloader in process 23953
Loading development environment (Rails 4.2.6)
2.2.1 :001 > Question.all.map{|s| puts s.body}
Question Load (0.4ms)  SELECT  "questions".* FROM "questions"  ORDER BY "questions"."id" DESC LIMIT 10
What is the supreme law of the land?
1) the Ley of the Land
2) the Article of Law
3) the Constitution
4) Article One of the Constitution
The "supreme law of the land" is not the Constitution.
1) True
2) False
What is the supreme law of the land?
1) the Ley of the Land
2) the Article of Law
3) the Constitution
4) Article One of the Constitution
The "supreme law of the land" is not the Constitution.
1) True
2) False
What is the supreme law of the land?
1) the Ley of the Land
2) the Article of Law
3) the Constitution
4) Article One of the Constitution
The "supreme law of the land" is not the Constitution.
1) True
2) False
What is the supreme law of the land?
1) the Ley of the Land
2) the Article of Law
3) the Constitution
4) Article One of the Constitution
The "supreme law of the land" is not the Constitution.
1) True
2) False
What is the supreme law of the land?
1) the Ley of the Land
2) the Article of Law
3) the Constitution
4) Article One of the Constitution
The "supreme law of the land" is not the Constitution.
1) True
2) False
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] 

这可能是数据库中遗留的旧数据...你可能想试试

db:reset

rails db:reset 任务将删除数据库并重新设置。这在功能上等同于rails db:drop db:setup。

导轨

导轨

最新更新