【问题标题】:what's a good way to implement this data structure in ROR activerecord?在 ROR activerecord 中实现这种数据结构的好方法是什么?
【发布时间】:2011-11-12 04:41:52
【问题描述】:

我不想在这里重新发明轮子,所以非常感谢一些建议!

我感觉实体->属性->价值系统有一个“标准”模型?

属性是为每个实体预定义的,因此在创建实例“Dave's Party”时,需要根据用户需要的“服务”向用户询问具体问题。例如。如果他需要照明,则需要询问他是否需要预算和颜色,以及他提供的值是否需要以正确的格式保存,或从一组预定义的选项中选择。

感谢您的帮助,并为我在下面列出数据的方式表示歉意,希望它是有意义的!


services = [stage,lighting,sound,tent]

lighting.info_types = [budget,colour]

lighting.size.type = options lighting.size.options = [small,medium,large]

lighting.info_types.colour.type = options lighting.info_types['colour'].options = [red,green,blue]

lighting.info_types.budget.type = currency


tent.info_types = [size,type,capacity]

....

stage.info_types = [size,powered,raised,stage_type]

....

“特定活动“戴夫的派对”需要小舞台,红色灯光预算为 300 英镑”

@event = Event.create :name => "Dave's Party"  
@event.services = [stage,lighting,sound]

照明有哪些 info_types 以及响应的正确格式是什么?

@event.services.lighting.size = large  
@event.services.lighting.colour = red  
@event.services.lighting.budget = 300.00

....

Dave's Party 需要哪些服务?每个服务的 info_types 和值是什么?

...

【问题讨论】:

    标签: ruby-on-rails activerecord types attributes data-modeling


    【解决方案1】:

    我会为灯光、声音、颜色、容量、预算类型等数据使用一堆表格和模型,并使用 ActiveRecord,然后您可以使用标准和熟悉的查询来管理数据。如果您有需要为应用程序初始设置的值,请​​使用 seed.rb 文件。您可以在几秒钟内将它们搭建起来以进行原型设计。

    其他人可能会推荐 sti/多态性,但除非您已经对这些领域相当有信心,否则我会推荐 KISS 并且只是分开表格。表格/模型在 Rails 中很容易做到!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2016-10-08
      相关资源
      最近更新 更多