lundi 30 mai 2016

Subcategories & Categories in Rails

I have the following setup to handle categories and sub categories.

Category.rb

class Category < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  has_many :subcategories
  has_many :products ,:through => :subcategories
end

Subcategory.rb

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

Product.rb

class Product < ActiveRecord::Base
  acts_as_taggable
  extend FriendlyId
  friendly_id :name, use: :slugged
  belongs_to :subcategory
end

Do I need to add a category_id:integer & subcategory_id:integer to the products model make it work, or does Rails handle this for me automatically?

Aucun commentaire:

Enregistrer un commentaire