I have Category
class shown below
class Category < ActiveRecord::Base
has_many :subcategories, class_name: "Category", foreign_key: "parent_category_id"
belongs_to :parent_category, class_name: "Category"
belongs_to :main_category
end
and I wonder if I can define main_category
association the rails way that I can
reference the #main_category
on subcategories but leaving the main_category_id
empty (as the reference on subcategory#main_category_id
will duplicate the data which is in parent_category#main_category_id
or it is just premature optimization? ).
category = Category.new main_category: main_category
subcategory = Category.new parent_category: category
assert_equal category.main_category, subcategory.main_category