2

faker gem を使用してデータをシードしようとしていますが、非常に奇妙な結果が得られます。カテゴリは gem を使用して入力されますが、記事は入力されません。関連付けを使用してデータベースにデータを入力することをテストしましたが、正常に動作します。実行時にエラー メッセージが表示されない

レーキ データベース:シード

ここに、seeds.rb ファイルがあります。

# Create 8 seed categories
categories = Category.create([
        { name: 'History'}, {name: 'Biology'}, {name: 'Literature'},
        { name: 'Mathematics'}, { name: 'Music Theory'}, { name: 'Computer Science'},
        { name: 'Sociology'}, {name: 'Chemistry'}
    ])

# create 50 articles, with random titles, 250 words of content, and
# randomly assign one of the categories above to each article
for i in 0..49
    title = Faker::Lorem.sentence(rand(2..10)).chomp('.')
    content = Faker::Lorem.paragraph(word_count=250)

    # randomly assign one of the categories we just created 
    category = Category.first(offset: rand(Category.count)) 
    a = Article.create(title: title, content: content, categories: [category,])
end

ここでも、カテゴリを作成するために機能しますが、記事は作成しません。以下は、db に使用されるすべてのモデルです。

class Article < ActiveRecord::Base
    belongs_to :user
    has_many :article_categories
    has_many :categories, through: :article_categories
    validates :title, presence: true
    validates :content, presence: true
    validates :categories, presence: true
end


class ArticleCategory < ActiveRecord::Base
    belongs_to :article
    belongs_to :category
    validates :article_id, presence: true
end


class Category < ActiveRecord::Base
    has_many :article_categories
    has_many :articles, through: :article_categories
    validates :name, presence: true
end
4

0 に答える 0