我遇到了在seeds.rb中使用我的创建数据方法找到问题的问题。我使用faker gem生成数据。 我的代码:
NAZWY_DNI = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela']
60.times do
WorkSchedule.create!(
start_time: Faker::Time.forward(1, :morning).strftime("%H:%M"),
end_time: Faker::Time.forward(1, :afternoon).strftime("%H:%M"),
day_of_week: NAZWY_DNI[Random.new.rand(0..6)],
person_id: Faker::Number.between(52, 112)
)
end
p "Created #{WorkSchedule.count} work schedules"
WorkSchedule表模型:
# == Schema Information
#
# Table name: work_schedules
#
# id :integer not null, primary key
# start_time :time not null
# end_time :time not null
# day_of_week :string not null
# created_at :datetime not null
# updated_at :datetime not null
# person_id :integer
#
当我尝试rake db:migrate
时,出现错误:ActiveRecord::RecordInvalid
。你知道我弄错了吗?提前谢谢。
完整追踪:
ActiveRecord::RecordInvalid: Nieprawidłowy rekord
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/validations.rb:43:in `save!'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:291:in `block in save!'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:220:in `transaction'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/transactions.rb:291:in `save!'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/persistence.rb:51:in `create!'
/home/lukas/Documents/aquapark/db/seeds.rb:84:in `block in <top (required)>'
/home/lukas/Documents/aquapark/db/seeds.rb:83:in `times'
/home/lukas/Documents/aquapark/db/seeds.rb:83:in `<top (required)>'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/engine.rb:547:in `load_seed'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:240:in `call'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/lukas/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/home/lukas/.rvm/gems/ruby-2.2.3/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/home/lukas/.rvm/gems/ruby-2.2.3/bin/rake:23:in `load'
/home/lukas/.rvm/gems/ruby-2.2.3/bin/rake:23:in `<main>'
/home/lukas/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
/home/lukas/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'
答案 0 :(得分:1)
在您的架构类型的start_time / end_time列中time
。但是,在你的种子中你有strftime("%H:%M")
调用,它将时间转换为字符串。因此,您不应该致电strftime
。
此外,您可以使用随机选择一天的NAZWY_DNI[Random.new.rand(0..6)]
代替NAZWY_DNI.sample
。而不是Faker::Number.between(52, 112)
只使用rand(52, 112)
。
答案 1 :(得分:0)
我发现了什么问题。在模型work_schedule.rb中,我进行了此验证:
validates_uniqueness_of :day_of_week, scope: :person_id
当我评论此行时,种子已加载。