私は9gagtvスタイル のような小さなビデオサイトのようにやっていて、サイト内のビデオにカテゴリがありますので、ユーザはすべてのTechビデオを見つけることができます。例えば ですが、ビデオはハイテクに関するものですが、面白いので、両方のカテゴリーのビデオにも表示されますが、どうすればよいか分かりません。 動画テーブルに複数のt.referencesが必要ですか?関係はどうやって行くの?カテゴリhas_manyビデオ、ビデオbelongs_to_manyカテゴリ
category.rb
class Category < ApplicationRecord
has_many :videos
end
video.rb
class Video < ApplicationRecord
belongs_to :category
end
カテゴリの移行
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :title
t.timestamps
end
end
end
ビデオ移行
class CreateVideos < ActiveRecord::Migration[5.0]
def change
create_table :videos do |t|
t.string :url
t.string :title
t.text :description
t.integer :duration
t.references :category, foreign_key: true
t.timestamps
end
end
end