The better model design for product in online-shopping store
Asked Answered
S

1

6

How to design the model for the same product with different color , size, quantity and price.

Here's my current model scheme,

Product
* Name
* Price
* Color
* Quantity
* Size

How to show the same product with different attributes in the same shopping section ?

Let me take the A&F shopping page for example,

When you visit this page, it means you are buying the COBBLE HILL TEE

I think the COBBLE HILL TEE with different color and size must be different different product instance in Product model, right ?

Like, the following three instances belongs to COBBLE HILL TEE but they are different instance in the model

 `COBBLE HILL TEE`, `$39`, `Red`, `1`, `XL`
 `COBBLE HILL TEE`, `$39`, `White`, `3`, `L`
 `COBBLE HILL TEE`, `$37`, `White`, `5`, `S`

So there should be a column to identify which products should be gathered into the same product, like COBBLE HILL TEE, right ?

Should I add a column called product_sn, when those records has the same value in product_sn, they should be gathered in the same shopping page ?

Sorry for my poor English to describe my question

enter image description here

Storax answered 8/6, 2014 at 3:14 Comment(1)
Your Current model schema is perfect.Go on with it.Montes
K
8

I love modularity in software, and create models accordingly. I think you'll benefit from this idea, so I'll explain it for you:


Models

I like to keep models to make models extensible - so you can add 1,000,000's of items, and still have it work in the right way.

To do this, we have "silo" databases (I'm not sure if this is the right terminology), and then have "reference" models around it.

The "silo" database / model basically stores static data (such as products or users). Reference database / models basically give the silo databases more scope - such as adding options to products or a profile for users.

In your case, I would definitely do this:

#app/models/product.rb
Class Product < ActiveRecord::Base
    has_many :options, as: :optable do
       def colours 
          where name: "colour"
       end
    end
end

#app/models/option.rb
Class Options < ActiveRecord::Base
    belongs_to :optable, polymorphic: true
end

Schemas:

#products
id | name | SKU | stock | etc | etc | created_at | updated_at

#options
id | optable_type | optable_id | name | value | created_at | updated_at

--

Associations

This is a polymorphic association (so you can use the options model with other unrelated models):

enter image description here

This means you'll be able to call:

@product = Product.find params[:id]
@product.options #-> returns all `Option` records for `product` (`price`, `size`, `etc`)

If you set it up right, you should be able to do this:

#config/routes.rb
resources :products, only: :show #-> domain.com/products/14

#app/controllers/products_controller.rb
class ProductsController < ActiveRecord::Base
   def show
       @product = Product.find params[:id]
   end
end

#app/views/products/show.html.erb
<%= @product.name %>
<% @product.options.each do |option| %>
    <%= option.size %>
    <%= option.price %>
    <%= option.colour %>
<% end %>

If you wanted to call a product's colour options, you could then do:

@product = Product.find params[:id]
@product.options.colours #-> will output all colours for the product

Caveats

One of the major caveats with my code is the options I've provided is not structured in any way. If you wanted to have a particular set of options for a product (like having size, colour, quantity for a particular product, you may wish to use a self-referential association in your Option model, which I can do if you want

Keratosis answered 8/6, 2014 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.