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
A consistent design that is in-keeping with the company's branding will build trust in customers. Consumers first impressions are 94% design-related and based on superficial elements such as colours and fonts. It is important to make sure all the pages use consistent fonts, font-sizes, colours etc…
1) Simplistic Web Design When it comes to designing an e-commerce store, the fewer elements you have on the page, the more confusing it's going to be for a potential customer. Keep your colors, ads, and pop-ups simple so that what's most important (your products) remains in focus.
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):
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With