简体   繁体   English

动态子属性

[英]Dynamic child properties

So I have need to have an architecture in place that allows for an object to have multiple child objects and those child objects have different dynamic properties. 因此,我需要有一个适当的体系结构,该体系结构允许一个对象具有多个子对象,并且那些子对象具有不同的动态属性。 Take this for instance: 以这个为例:

restaurants table
-----------------
id int primary_key unique not null
name varchar(50) not null

restaurants_equipment table
---------------------------
restaurant_id int
equipment_id int
??? JSON values of properties ???

equipment table
---------------
id int primary_key unique not null
name varchar(50) not null
??? JSON of available properties per equipment ???

In the table above the "???" 在表格上方的“ ???” indicates if this the best approach. 指示这是否是最佳方法。 See explanation below. 请参阅下面的说明。

So imagine, if you will and for simplicity sake, that there are 2 restaurant records, and 3 equipment records. 因此,想象一下,如果您愿意并且为了简单起见,有2条餐厅记录和3条设备记录。 The three records in the table are "Oven, Microwave, and Stove". 表中的三个记录是“烤箱,微波炉和炉子”。 Oven has properties that need to be stored of max_temperature (integer), racks_space (array of rack sizes). 烤箱具有需要存储的属性,这些属性包括max_temperature(整数),racks_space(机架尺寸的数组)。 Microwave has properties of max_timer (integer), popcorn_setting (boolean). 微波具有max_timer(整数),popcorn_setting(布尔值)的属性。 Stove has properties of num_of_tops (integer), stove_type (integer). 炉具的属性为num_of_tops(整数),stuff_type(整数)。

As you can see, each piece of equipment can have any type of dynamic properties that also need to be tied to the restaurant with the values they may have for that piece of equipment. 如您所见,每台设备都可以具有任何类型的动态属性,这些属性也需要与餐厅联系在一起,并具有它们对于该设备可能具有的值。

So, my question is what is the best approach to have dynamic properties per record in equipment and those values to be stored appropriately in a relationship between the equipment and particular restaurant AND be able handle constraints with this properties for user input (ie store constraint information, as well, etc?)? 因此,我的问题是什么是使设备中的每个记录具有动态属性,以及将那些值适当存储在设备与特定餐厅之间的关系中,并能够使用此属性处理用户输入约束的最佳方法(即存储约束信息) ,等等?)?

You have 2 options: 您有2个选择:
Options #1: (for many equipments) 选项1 :( 对于许多设备)
If the data is for viewing only , you can save the equipments specifications in JSON format in a text field , and parse the string to JSON format, either at the backend (Model) or at the front end (javascript or other library) , to show the data in your ERB file. 如果数据仅用于查看 ,则可以在文本字段中JSON格式保存设备规格,然后在后端(模型)或前端(javascript或其他库)将字符串解析为JSON格式,以在您的ERB文件中显示数据。

Option #2: (limited number of equipments) 选项2 :( 设备数量有限)
Create table for each Equipment specification, and use polymorphic association . 为每个设备规范创建表,并使用多态关联

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

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

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