Rails3产品组

时间:2011-02-25 00:02:47

标签: ruby-on-rails ruby-on-rails-3

需要帮助解决这个问题。

拥有产品型号:

class Product 
   :name 
    :short_description 
    :price 
    :cost 
    :visible 
    :new_service 
    :deleted 
    :category_id 
 end

拥有订单项型号。除了以下几乎复制产品型号:

class LineItem
    :order_id 
    :customer_id
    :company_id
end

现在需要弄清楚如何添加一个产品组,该产品组将由多个产品组成,并带有与产品型号相同的所有产品。通常我会使用:ids做一个连接表,但需要能够编辑组中每个产品的价格和成本。

难倒这个。

3 个答案:

答案 0 :(得分:0)

您是否表示集团内的每个产品的价格可能与集团外的正常价格无关(例如,捆绑产品可能会打折)?

如果是这样,那是不是暗示了ProductGroup模型和ProductGroupProduct(shudder)模型?

编辑:你也可以挖掘Spree的想法:

https://github.com/spree/spree

答案 1 :(得分:0)

class ProductGroup
  has_many :special_products
end

class SpecialProducts
  belongs_to :product
  belongs_to :product_group

  #attributes for overridden prices
  #delegate other attributes to product
  delegate :name, :to => :product, :allow_nil => true
end

class Product
  has_many :special_products
  has_many :product_groups, :through => :special_products
end

答案 2 :(得分:0)

我不知道是否得到它,如果您需要将一组产品与ProductGroup关联,为什么不在ProductGroup模型中使用“:has_many products”,在Product model中使用“:belongs_to ProductGroup”

然后,如果您需要编辑属于某个ProductGroup的每个产品的价格,您可以执行以下操作:

products = ProductGroup.find(2).products
products.each do |product|
  product.price = 12.00
  # More code...
end

或类似的东西。