Search code examples
ruby-on-railsdatabaseschemamodels

Rails database schema


Caveat: I'm new to rails and app development in general so please excuse any noobness.

I'm trying to figure out the database schema for my app. I've got the following models: Company, User(employee), Product, Client and Order.

Background: The company has a database which is maintained by users(employees). The client can visit the company site, login and then access the database to see the products and their stock levels. From this they can then make a purchase i.e. an order. I've got the following schema with these associations:

  • Company: has_many users, has_many products, has_many clients, has_many orders through clients.
  • User(employee): belongs_to Company
  • Product: belongs_to Company
  • Client: belongs_to Company
  • Order: belongs_to Company through clients

Is this correct or am I missing something? Would be grateful for any advice. Thanks.


Solution

  • I believe you are indeed missing some tables.

    Do you need categories ? You need a categories table.

    can you order have many products ? you need another table (1 order has many order details ).

    How are you going to store product details ? you need a table there as well if you have multiple size for each products for example.

    I also don't really understand why do you need a client model vs using your user model for clients ?

    Company has many :clients,class_name:"User"