Stock_QTY范围缩小的产品型号

时间:2014-11-06 17:11:51

标签: ruby-on-rails ruby ruby-on-rails-4 scope

我有一个功能强大的自建e-com网络应用程序,但现在该应用假设我们拥有无限数量。

它使用line_items和产品模型。

我将添加stock_QTY作为产品的属性

对于没有任何变体(尺寸,颜色等)的项目,如果且stock_QTY大于1,则会创建line_item。

我不知道如何处理尺寸。

我应该创建不同的产品吗? IE:

Shirt.create (name:"small green shirt", color:"green", size:S, stock_QTY:4)
Shirt.create (name:"medium green shirt", color:"green", size:M, stock_QTY:6)
Shirt.create (name:"large green shirt", color: "green", size:L, stock_QTY:1)

这似乎是重复的,但至少库存数量可以有一些独立性。有没有办法只创建一个带有变体的衬衫记录,并允许它们有不同的尺寸?

理想情况下我想

Shirt.create(name:"shirt", colors:['red', 'blue', 'green'], sizes: ['s','m',l'])

然后能够做到

Shirt.where(color => "green").where(size => "L").stock_QTY
=> X number
Shirt.where(color => "green").where(size => "M").stock_QTY
=> Y number

这种方式我有一个型号,但它可以存储不同的数量,具体取决于变体的范围。 如果不清楚,请告诉我。

谢谢!

更新 Product.rb

require 'file_size_validator'
class Product < ActiveRecord::Base
    has_many :line_items
    before_destroy :ensure_not_referenced_by_any_line_item
    mount_uploader :image, ImageUploader
    validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
    validates :title, :uniqueness => true
    def to_param
      "#{id}_#{permalink}"
    end
    private

    # ensure that there are no line items referencing this product
    def ensure_not_referenced_by_any_line_item
      if line_items.empty?
        return true
      else
        errors.add(:base, 'Line Items present')
        return false
      end
    end
end

这是我现在的产品。 来自seeds.rb

Product.create!([
{
    :title => "Liaisons Shirt Green",
    :description => "",
    :has_size => true,
    :price => 24.99, 
    :permalink => "shirt",
    :weight => 16.00,
    :units => 1.00,
    :image => (File.open(File.join(Rails.root, "app/assets/images/dev7logo.png")))

}
])

2 个答案:

答案 0 :(得分:1)

所以,我的建议是改进数据库架构,使其更加灵活和可扩展;)

定义大小和颜色模型(2个新表),使您的实际产品模型成为BaseProduct模型(只是重命名表),最后创建将具有3个外部键的产品模型(新表)(base_product_id,color_id和size_id)当然还有stock_qty字段,用最少的重复信息来定义所有可能的配置:)!

只需要一点帮助,你的最终类架构应该是:

class Color < ActiveRecord::Base
end

class Size < ActiveRecord::Base
end

class BaseProduct < ActiveRecord::Base
   # This will have almost all fields from your actual Product class
end

class Product < ActiveRecord::Base
   # Ternary table between Color, Size and BaseProduct
end

我省略了所有关联,因为我喜欢你自己成功的想法,但如果你需要,请问问:) 这将允许您执行BaseProduct查询,如:

base_product.colors
base_product.sizes
product.base_product # retrieve the base_product from a product

并跟踪数量:

product.stock_qty
product.color
product.size # size and color are unique for a specific product

您还可以创建一些辅助方法,使创建过程与您想要的过程类似(如您的问题所示)。

答案 1 :(得分:0)

我理解您想要处理的方法。如果我理解正确的话,这很容易理解。所以你想要以下的事情如果我找到你的话:

  1. 你有这么多产品
  2. 您想要添加库存盘点记录
  3. 如果产品可用
  4. ,您希望验证产品的销售情况(购物车的订单项)
  5. 当您删除产品时,您需要确保产品是否已经在客户的购物车中。
  6. 所以我假设你已经添加了stock_qty列。

    现在您需要确保产品是否可以添加到购物车中。

    因此,您需要在line_item调制解调器中编写验证。

    class LineItem < ActiveRecord::Base
     # other business logics are here
     belongs_to :product
     before_validation :check_if_product_available
    
     def check_if_product_available
         # you will find your product from controller, model should be responsible to perform business  
         # decision on them.
         if !self.try(:product).nil? && self.product.stock_qty < 1
             errors.add(:product, 'This product is not available in the stock')
             return false
         end
     end
    end
    

    这是我认为有效的方法。而且,相反,在相同的产品模型中保存变体,我建议考虑使用单独的变体模型更有效地设计模型,或者您可以利用自我关联的力量。

    我希望这会对你有所帮助。如果我遗漏任何内容或错过解释您的问题,请告诉我。

    由于