多种产品类型购物车的Rails方式

Dea*_*ean 9 activerecord ruby-on-rails ruby-on-rails-3.2

我正在设计一个管理许多不同设备租赁的应用程序.我想知道什么是为应用程序设计模型的最佳方法.我的软件必须管理许多不同类型的设备(包括数据类型),例如:

Speaker
  Make - String
  Model - String      
  Wattage - Integer
  Price - Decimal

Light
  Make - String
  Model - String      
  Wattage - Integer
  Price - Decimal

Microphone
  Make - String
  Model - String
  Use - Choice of: Instrumental, Vocal, Versatile
  Price - Decimal

Cable
  Length - Decimal
  Connector 1 - String
  Connector 2 - String
  Price - Decimal

Stand
  Type - Choice of: Microphone, Speaker
  Height - Decimal
  Boom - Boolean
  Price - Decimal
Run Code Online (Sandbox Code Playgroud)

我考虑过设计的方式:

  • 每种类型产品的单独模型,然后购物车中的多态关联,以便它可以处理所有类型的设备.
  • 单个产品型号,具有适用于所有类型设备的字段,其类型字段可在使用产品时进行检查.
  • 具有price属性的产品模型然后每种类型的产品扩展该模型.

但是铁路处理这些不同类型产品的最佳方式是什么?

sea*_*boy 4

动态属性 gem 应该允许您自动执行此操作:

https://github.com/moiristo/dynamic_attributes

可能有更好的宝石可以满足您的需求,但这是我发现的第一个。

如果您使用 Postgres 作为数据库,那么您可以使用hstore。有一些宝石可以与 hstore 一起使用。如果您负担得起,请订阅 Railscast 并观看有关实施 hstore 的截屏视频

Activerecord-postgres-hstore似乎是解决这个问题的最佳选择。