简体   繁体   English

在线购物商店的产品更好的模型设计

[英]The better model design for product in online-shopping store

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, 让我以A&F购物页面为例,

When you visit this page, it means you are buying the COBBLE HILL TEE 当您访问此页面时,这意味着您正在购买COBBLE HILL TEE

I think the COBBLE HILL TEE with different color and size must be different different product instance in Product model, right ? 我认为不同颜色和尺寸的COBBLE HILL TEE必须是产品型号中不同的产品实例,对吗?

Like, the following three instances belongs to COBBLE HILL TEE but they are different instance in the model 比如,以下三个实例属于COBBLE HILL TEE但它们在模型中是不同的实例

 `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 ? 所以应该有一个专栏来确定应该将哪些产品收集到同一产品中,比如COBBLE HILL TEE ,对吗?

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 ? 我应该添加一个名为product_sn的列,当这些记录在product_sn具有相同的值时,它们应该收集在同一个购物页面中吗?

Sorry for my poor English to describe my question 抱歉我的英语不好用来描述我的问题

在此输入图像描述

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. 我喜欢保留模型以使模型可扩展 - 因此您可以添加1,000,000个项目,并且仍然以正确的方式工作。

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 ). “筒仓”数据库/模型基本上存储静态数据(例如productsusers )。 Reference database / models basically give the silo databases more scope - such as adding options to products or a profile for users . 参考数据库/模型基本上为silo数据库提供了更多范围 - 例如向products添加options或为users添加profile

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): 这是一个多态关联 (因此您可以将options模型与其他不相关的模型一起使用):

在此输入图像描述

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: 如果您想调用productcolour选项,则可以执行以下操作:

@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. 我的代码的一个主要警告是我提供的options没有任何结构。 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 如果您想为产品set一组特定的选项(例如具有特定产品的sizecolourquantity ,您可能希望在您的Option模型中使用self-referential关联 ,如果您愿意,我可以这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM