【问题标题】:Seed database with foreign key - seeds.rb带有外键的种子数据库——seeds.rb
【发布时间】: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


【解决方案1】:

我可以看到两个问题:

  1. Movie.create 中,您需要删除方括号。那是因为 ActiveRecord create 方法需要一个哈希,而不是一个数组。当您使用它时,您也可以删除花括号,您的参数仍将被识别为哈希。

  2. 检查movie 是否为nil 不是正确的测试,因为movie 将始终存在,即使它没有保存到数据库中。将您的测试从 unless movie.nil? 更改为 if movie.persisted? 这将避免由于尝试将 cmets 保存到未保存到数据库中的电影而导致的错误。

所以你的代码应该是这样的:

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)])
  if movie.persisted?
    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

为什么有些电影没有被保存?我怀疑你的电影片名用完了,然后你的唯一性约束就失败了。 Faker 对任何类别都有有限数量的响应。我认为只有 22 个独特的 Faker::Book.title 回复。

【讨论】:

  • 它立即生效,我只需要删除唯一性验证。感谢您的帮助和快速响应
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2015-07-01
  • 2020-02-12
  • 2020-09-17
  • 2012-07-21
  • 1970-01-01
相关资源
最近更新 更多