我正在尝试用一个简单的种子文件填充我的sqlite3数据库,该文件应该在Film表中创建一堆电影条目,然后为存储在Comments表中的这些电影创建一些注释.
formats = %w(Beta VHS IMAX HD SuperHD 4K DVD BlueRay) 30.times do film = Film.create(title: "#{Company.bs}",director: "#{Name.name}",description: Lorem.paragraphs.join("<br/>").html_safe,year: rand(1940..2015),length: rand(20..240),format: formats[rand(formats.length)] ) film.save (rand(0..10)).times do film.comments.create( author: "#{Name.name}",title: "#{Company.bs}",content: Lorem.sentences(3).join("<br/>").html_safe,rating: rand(1..5) ) end end
一旦我执行rake db:seed我就不可避免地得到了错误
ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
我的film.rb文件是
class Film < ActiveRecord::Base has_many :comments validates_presence_of :title,:director validates_length_of :format,maximum: 5,minimum:3 validates_numericality_of :year,:length,greater_than: 0 validates_uniqueness_of :title paginates_per 4 end
使用从“格式”列表中选择的格式创建电影时,“格式”的长度限制会引发错误