Search code examples
mysqlruby-on-railsdatabaserelational-databasedatabase-schema

Database for an online store


I'm about to make an online e-commerce site for selling some products like footwear,clothes etc. but I'm unable to figure out how to set up the schema for that (tables and all). I'm doing it in Ruby on Rails with Mysql and I'm newbie in ROR.

Details are as follows:-

I have Different categories in products like Footwear,Clothing,Accessories,Bags,etc separate each for Men and Women.

The buyer selects the products and add it to cart and proceed to chekout. We just have a cash on delivery type Payment system. We wont be creating any user accounts.No user accounts

How should I achieve that in Rails with all the controllers and the models?
Should I make separate table for each of the category?
How should I categories the required Products in Men and Women?
Provide a complete schema and relationships table the Rails way.


Solution

  • You'll need Categories table and Products table. In products table you'll need category_id:integer field.

    Category.rb

    has_many :products
    

    Product.rb

    belongs_to :category
    has_many :line_items
    

    Cart.rb

    has_many :line_items
    

    LineItem.rb (table line_items with product_id:integer and cart_id:integer)

      belongs_to :cart
      belongs_to :product
    

    each time someone adds a product to cart it will create a line_item row with product_id and cart_id and you can also add quantity field to update if customer order more products of the same type.

    For Men and Women types of products you could add a field in products table that will id id this product is for him or for her, for_men: boolean, default:true

    Product.rb

    belongs_to :category
    has_many :line_items
    
    scope :men, -> {where(for_men: true)}
    scope :women, -> {where(for_men: false)}
    

    and when if you call

     products = Product.all
     products.women
    

    it will show you only products marked as women type, where for_men field is false.

    In case when you extract products by category:

    category = Category.find 1
    
    category.products.women 
    

    will show you products for women of that category.