I'm trying to populate my SQLite3 database with a simple seed file that is supposed to create a bunch o movies entries in the Film table and then create some comments to this movies that are stored in Comments table.
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
Once i execute rake db:seed I inevitably get the error
ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
and no records are added to either Films or Comments
My film.rb file is
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
The length limit on 'format' raises the error when creating a Film with formats selected from the 'format' list
Film.create
toFilm.create!
and you should which validation prevents film from getting saved. – CleanshavenActiveRecord::RecordInvalid: translation missing: ro.activerecord.errors.messages.record_inval
– Circumjacentrecord_invalid
? And are you sureCompany.bs
andName.name
works as expected? Also, I strongly recommend avoid usinglength
as Film attribute name, since it's also ruby method and it might end up causing really strange situations. – Cleanshaven.new
on the model rather than.create
whoops. – Ephedrine