电子商店应用程序的模型关联

发布于 2024-12-06 16:22:06 字数 346 浏览 1 评论 0原文

我正在使用 ruby​​ on Rails 开发一个电子商店应用程序,我对模型关联有点困惑。如果有人能给我一些关于表格及其关联的想法,这会对我有所帮助。详细信息如下:

父表: =>类别, 场合, 类别下的热销商品

: => 男士, 女性, 男人下的孩子

: =>衬衫, 长裤

女式 : =>衬衫, 裤子, 裙子

儿童 : =>衬衫, 裤子

适合场合的 => 种族, 派对, 旅行, 随意的, 正式

热销中 =>Hot Deals Index

最后每个子表都会有产品索引。

谢谢你!

I'm developing an e-store app using ruby on rails and I'm a bit confused about the model associations. It would help me if someone could give me an idea about the tables and their associations. Here are the details:

Parent tables:
=>Categories,
occasions,
Hot Deals

Under Categories:
=>Men,
Women,
Kids

Under Men:
=>Shirts,
Trousers

Under Women:
=>Shirts,
Trousers,
Skirts

Under Kids:
=>Shirts,
Trousers

Under Occasions
=>Ethnic,
Party,
Travel,
Casual,
Formal

Under Hot Deals
=>Hot Deals Index

And lastly every last sub-table will have product index.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2024-12-13 16:22:06

对于这样的事情,您通常会做的是构建一个与产品相关联的分类树,从而允许您将产品分组在一起。分类法和产品之间的多对多关系让您可以将产品与多个组关联起来,因此一件 T 恤可能位于“类别”>“类别”下。男士>衬衫以及场合>随意的。

# app/models/taxonomy.rb
class Taxonomy < ActiveRecord::Base
  has_many :taxonomies
  has_and_belongs_to_many :products
end

# app/models/product.rb
class Product < ActiveRecord::Base
  has_and_belongs_to_many :taxonomies
end

然后迁移分类法/产品之间的连接表

rails g migration create_products_taxonomies

并对其进行编辑。

def change
  create_table(:products_taxonomies, :id => false) do |t|
    t.references :product
    t.references :taxonomy
  end
end

从那里,您基本上将在数据库中创建 3 个分类单元,每个部分 1 个,然后创建分类法并构建子级别。创建产品时,请为产品和您的产品集分配正确的分类法。

种子文件可能看起来像...

Taxonomy.create!(:name => "Category").tap do |category|
  category.taxonomies.create!(:name => "Men").tap do |men|
    men.taxonomies.create!(:name => "Shirt")
    men.taxonomies.create!(:name => "Trousers")
  end
  # and so on for each category
end

然后,当您创建产品时,您可以将其与分类法关联起来,并使用该分类法来提取与其关联的产品列表。

What you would generally do with something like this is build a Taxonomy tree to that would be associated with the products, allowing you to group products together. A many to many relation between Taxonomy and Product let's you associate a product with multiple groups, so a T-Shirt might be under Categories > Men > Shirts as well as Occasion > Casual.

# app/models/taxonomy.rb
class Taxonomy < ActiveRecord::Base
  has_many :taxonomies
  has_and_belongs_to_many :products
end

# app/models/product.rb
class Product < ActiveRecord::Base
  has_and_belongs_to_many :taxonomies
end

then a migration for the join table between taxonomy/product

rails g migration create_products_taxonomies

and edit it

def change
  create_table(:products_taxonomies, :id => false) do |t|
    t.references :product
    t.references :taxonomy
  end
end

From there you would basically create in the database 3 Taxons, 1 for each of your sections, then create the Taxonomy and build out the sub levels. When you create your products, assign the right Taxonomy to the product and your set.

A seed file might look like...

Taxonomy.create!(:name => "Category").tap do |category|
  category.taxonomies.create!(:name => "Men").tap do |men|
    men.taxonomies.create!(:name => "Shirt")
    men.taxonomies.create!(:name => "Trousers")
  end
  # and so on for each category
end

Then when you create a Product, you can associate it with the Taxonomy and use that Taxonomy to pull up a list of products that are associated with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文