类别、子类别和演出之间的关联

Association between category,subcategory and gig

我的网站有类别->子类别->演出 (by gigs i mean lots of adds)

以下是3个模型之间的关系。

class Category < ActiveRecord::Base
  has_many :subcategories
end


class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :gigs
end


class Gig < ActiveRecord::Base
  belongs_to :user
  belongs_to :subcategory
end

问题:

What rails migrations should i use to make the required relationship between them in schema.rb,for example ("should i create category_id,or subcategory_id,or gig_id...),if yes in what table should they go,i got quite confused about it.

感谢您的宝贵时间。

"belongs_to" 关系总是需要 table 上的外键。

话虽如此,您的 演出 table 应该

t.integer :user_id
t.integer :subcategory_id

你的子类别table应该有

t.integer :category_id

希望对您有所帮助!