-2

我的商店需要 2 个模型:

  1. 鞋子
  2. 鞋号

根据我的概念 - 一只鞋可以有多种尺寸(一种型号的鞋可以是 34、35、36、37 尺寸)。

我应该使用什么类型的关联?我需要创建哪些数据库表文件来支持这些关联?

以下是使用的模型示例:

Shoe.find(1).shoe_sizes => 34,35,36

ShoeSize(2).shoes => #Shoe1, #Shoe2, #Shoe5
4

1 回答 1

1

你应该使用has_and_belongs_to_many关系。

class Shoe
  has_and_belongs_to_many :shoe_sizes
end

class ShoeSize
  has_and_belongs_to_many :shoes
end

这是因为一只鞋可以有多种鞋码,同样一种鞋码可用于不同的鞋。

于 2012-01-07T18:42:09.827 回答