【发布时间】:2016-10-21 17:13:23
【问题描述】:
我正在做一个像 9gagtv 风格的小视频网站 并且网站中的视频有一个类别,因此用户可以找到所有的技术视频,例如 但是有些视频属于_to_many 类别,例如当视频是关于技术的但也很有趣,所以它会出现在这两个类别的视频中,我不知道该怎么做? 在视频表中需要多个 t.references 吗?感情会怎样?
category.rb
class Category < ApplicationRecord
has_many :videos
end
视频.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
【问题讨论】:
标签: ruby-on-rails model-associations