Let’s say customer is adding multiple products to cart.
Add car, apartment, tour, some coupons.
Products belong to categories.
Category has name attribute.
But how to give different attributes to car, apartment, tour, coupon?
I can’t definitely create everything from product model.
So should I create different models for each category and connect through has many ro products model?
Or am I going to wrong direction?
Thanks
Let’s say customer is adding multiple products to cart. Add car, apartment, tour, some
Share
Having a Category for individual product seems to be a good approach here. Since your
Categorydiffers according to attributes. What you can do is create One more model, let’s say:MasterCategorywhich has manyCategoryandCategorybelongs toMasterCategory, which means for yourMasterCategoryCarsyou’ll haveAudi, BMW, Nissan etc etc. categoriesand then link your products to their respective vendors in a tableproducts_categorieswhich will haveproduct_id, category_id.In my opinion, schema could go like this:
For e.g. – A
carMasterCategory will look like this:and its
categorieswill be:Now you can have two methods
parentandchildrenin yourCategoryModel usingparent_idattribute to locate and traverse master and sub categories easily. And you can usepermalinkattribute to easily locate a category which is under another category with one query:Category.find_by_permalink(params[:permalink])and can display all the products related with this particular category. In future when you’ll scale, I can bet you, you’ll require thispositionattribute to manage position of your categories to display on your page. And in lastmaster_category_idwill give you an ease in life with:All the best!! 🙂