【发布时间】:2018-03-16 08:50:17
【问题描述】:
我正在尝试使用 Faker gem 播种数据库,当我尝试仅播种电影时我成功了,但是当我尝试播种电影时,每个电影都有 1-10 cmets 我得到一堆不同的错误取决于什么我变了。
这是我的seeds.rb 的样子:
require 'faker'
formats = %w[Beta VHS IMAX HD SuperHD 4K DVD BluRay]
genres = %w[Triller Comedy Horror Action Drama SciFi Documentary]
images = %w[magento.svg mysql.svg php.svg jquery.svg mongodb.svg prestashop.svg meteor.svg]
Movie.destroy_all
Comment.destroy_all
100.times do
movie = Movie.create([{ name: Faker::Book.title,
director: Faker::Name.name,
description: Faker::FamilyGuy.quote,
year: rand(1920..2018),
length: rand(80..240),
format: formats[rand(formats.length)],
genre: genres[rand(genres.length)],
image: images[rand(images.length)],
thumbnail: images[rand(images.length)] }])
unless movie.nil?
rand(1..10).times do
movie.comments.create(
author: Faker::Name.name,
title: Faker::Book.title,
content: Faker::FamilyGuy.quote,
rating: rand(1..5)
)
end
end
puts movie.inspect
end
这是我的评论模型:
class Comment < ApplicationRecord
belongs_to :movie
end
这是我的电影模型:
class Movie < ApplicationRecord
has_many :comments
validates_presence_of :name, :director
# validates_numericality_of :year, :length, greater_than: 0
validates_uniqueness_of :name, message: 'Name is already used!'
# validates_length_of :year, maximum: 4
paginates_per 10
def proper_name
name.titleize
end
end
感谢您的帮助。
【问题讨论】:
-
你使用的是什么版本的 Rails?
标签: ruby-on-rails ruby foreign-keys seeding faker