我有一个家长ProductCategory
和子女Product
。例如:如何有效地搜索字符串数据?
ProductCategory --- Product
Drill --- DeWalt DWD 112
--- Black & Decker 5 C
Bicycle --- Motobecane Turino ELITE Disc Brake
--- Shimano Aluminum
对于给定的ProductCategory
,有一组属性的所有Products
应与彼此可比较(即,对数据)。但是,这组属性可能会在ProductCategories
之间变化。例如,对于Drill的ProductCategory
,属性可能为Voltage, Amps, Corded vs Cordless
。每个Product
钻取需要有这样的信息。但是,对于自行车的ProductCategory
,属性应为Size, Road vs Mountain
,并且每辆Product
自行车都需要具有此信息。 (对不起,我不知道任何关于钻子或自行车...为什么我选择这是愚蠢的)
我试图设计的数据库,如果给定Product
,属性是我可以很容易搜索。例如,理想情况下,我可以运行此命令:
drills = Product.where(product_category_id:1)
drills.where("voltage >= ?", 5)
-> returns the individual drills, which may include DeWalt but not Black & Decker
这似乎提出了一个有趣的权衡......因为那时我得有Product
有列,每ProductCategory
每个属性,即使是那些与它无关。例如:
# Product columns
:voltage, :integer #for Drill
:amps, :integer #for Drill
:corded, :boolean #for Drill
:size, :integer #for Bicycle
:mountain, :boolean #for Bicycle
...
这似乎不是可持续的......你可以看到非常快,只需几ProductCategories
很快就会有Product
列无限多的!
在光谱的另一端,我想是每一个Product
的所需属性定义在父ProductCategory
,然后请求这些属性/上Product
存储它们作为一个字符串化数据:
# ProductCategory has a column...
:required_attributes, :text
ProductCategory.where(name:"Drill").first.required_attributes
-> "voltage,amps,corded"
ProductCategory.where(name:"Bicycle").first.required_attributes
-> "size,mountain"
# Product has a column...
:attribute_data, :text
Product.where(name:"DeWalt").first.attribute_data
-> "{'voltage':5,'amps':5,'corded':5}"
随着在上面的设计中,我可以创建一个前端,在创建Product
时,必须为每个required_attributes
在根据逗号分割后提供信息。但是,当然,这使得搜索效率低得多,至少我认为它......所以这是我的问题。 如何有效地搜索串化数据?如果我正在搜索至少5伏的所有演习,请完成以下步骤。
drills = ProductCategory.where(name:"Drill")
drills.where("attribute_data ...")
查看更新的答案。 –
我删除了我的答案,因为这不是一个很好的答案。但是,这不是一个真正可以在SO上回答的问题,因为它非常广泛,并且需要关于现有模式(如EAV表和JSON列)的一些知识。 – max
我得到@max,但我发现其中的参考有用,我正在探索这个公认的广泛主题。即使作为评论,你是否介意重新发布? – james