连接类别、子类别和演出之间的关系

Connect the relationship between category,subcategory and gig

我的应用程序有类别,而不是子类别,而不是演出

我的控制器文件中有 gigs_controller.rb

和 category.rb、subcategory.rb、gig.rb 在我的模型文件中。

如何按照类别=子类别=演出的原则建立他们之间的联系(演出我的意思是我有很多小的补充)

在Category.rb模型

中这样说对吗
class Category < ActiveRecord::Base
  has_many :subcategories
end

在我的子类别模型中

class Subcategory < ActiveRecord::Base
  belongs_to :category
end

在我的 Gig.rb

class Gig < ActiveRecord::Base
  belongs_to :user
  belongs_to :category # I also need here to be belongs_to :subcategory,how do i do that?
end

我应该为类别和子类别创建一个控制器吗?你会怎么做。 谢谢大家的支持,希望我的问题对其他人也有帮助。

是的 你可以在模型中使用 has_many 而无需 controller.Because has_many 使用 table 的名字。

class Gig < ActiveRecord::Base

belongs_to :user

belongs_to :subcategory

end

class Category < ActiveRecord::Base

has_many :subcategories

end

class Subcategory < ActiveRecord::Base

belongs_to :category

has_many :gigs

end

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