我想用 gems populator 和 faker 填充我的数据库。我只是按照railscast 教程。如果我只是按照教程进行操作,我的代码如下所示:
namespace :db do
desc "fill compact tables with sample data"
task :populate => :environment do
require 'populator'
require 'faker'
[RepositoryCompact, BuildCompact, JobCompact].each(&:delete_all)
RepositoryCompact.populate 10 do |repository|
repository.name = Faker::Name.name
repository.description = Populator.sentences(1)
repository.owner_name = Faker::Name.name
repository.url = Faker::Internet.url
i = 0
BuildCompact.populate 1..10 do |build|
build.config = Populator.sentences(1..3)
build.repository_compact_id = repository.id
build.number = i
i++
build.result = 0 || 1
build.finished_at = 2.years.ago..Time.now
JobCompact.populate 1..5 do |job|
job.build_compact_id = build.id
job.allow_failure = 0 || 1
#job.finished_at = build.finished_at-(2..100)
job.language = [ruby, php, java, c, python, rubinius, jruby]
job.version = [1.9 .. 2.5]
job.result = 0 || 1
end
end
end
end
end
如果我运行rake db:populate
关系,gem () 似乎无法识别该关系undefined method repository_compact_id=for #<Populator::Record:0x0000010177ffb8>
但即使将除 RepositoryCompact 部分之外的所有内容都注释掉,rake 也会中止Validation failed: Name has already been taken
我确信我设置了正确的模型和关联。有没有明显的错误?
会不会是宝石populator
太旧了?
提前致谢!