你可能想在超市和详细的产品之间建立多对多的关系。详细的产品将具有一般产品的外键。
假设您命名通用产品模型属(复数:属)。[请随意查找更好的名称。]
class Genus
has_many :products
end
class Brand
has_many :products
end
class Product
belongs_to :genus
belongs_to :brand
has_many :supplies
has_many :supermarkets, :through => :supplies
end
class Supply
belongs_to :product
belongs_to :supermarket
end
class Supermarket
has_many :supplies
has_many :products, :through => :supplies
end
Product Genus
Granny Smith Apple Apple
Golden Delicious Apple Apple
Braeburn Apple Apple
Macintosh Apple Apple
Kellog's Cornflakes Cornflakes
GM Cornflakes Cornflakes
Kellog's Rice Krispies Rice Krispies
你还可以在“产品到品牌”、“凯洛格商店”或“通用磨坊”中加入一把外国钥匙。