2016-01-21 88 views
0

我是一位初级开发人员,他刚刚参加了第一个rails项目。我面临很多障碍,但从中学到很多东西。到目前为止,我能够找出Devise以及所有好东西中的多个用户。但对授权半自信。我仍然继续学习,并相信我会弄清楚。Rails建模帮助,关联和逻辑

但是我过去一周唯一的砖墙时刻是为我的应用程序建模以进行订单部分。下面是应用程序的一个小小的总结我的工作:它的B2B

  1. 用户类型是零售商和供应商
  2. 零售商下订单与供应商
  3. 只有一个产品有3种不同类型或更多,product_type1,product_type2,product_type3
  4. 供应商每天更新产品类型的价格,零售商看到其仪表板中的当前价格
  5. 供应商也有每个产品的公式每个零售商的价格。

例如,他的底价+保证金,他的保证金是不同的零售商。

那么我该如何建模?我希望零售商以各自的价格向供应商下订单。

我需要什么?

产品型号?随着价格和类型?

独立公式模型?

+1

需要一些时间并从头到尾完成[Rails Guides](http://guides.rubyonrails.org/getting_started.html)并执行给出的所有示例。 –

+0

欢迎来到Stack Overflow。我强烈推荐阅读http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/和http://catb.org/esr/faqs/smart-questions.html。另外,程序员在问之前花了很多时间研究和尝试;很多时间。我们期望我们的同行; http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users –

回答

2

我理解你的感受,因为我之前有完全相同这个问题,让我分享我做什么,希望它可以帮助您解决问题:

User型号:

class User < ActiveRecord::Base 
    # A user is a registered person in our system 
    # Maybe he has 1/many retailers or suppliers 
    has_many :retailers 
    has_many :suppliers 
end 

Order型号:

class Order < ActiveRecord::Base 
    # An order was placed for supplier by retailer 
    belongs_to :retailer 
    belongs_to :supplier 
    # An order may have so many products, we use product_capture 
    # because the product price will be changed frequently 
    # product_capture is the product with captured price 
    # at the moment an order was placed 
    has_many :product_captures 
end 

Product型号:

class Product < ActiveRecord::Base 
    belongs_to :retailer 
    has_many :product_captures 
    # Custom type for the product which is not in type 1, 2, 3 
    enum types: [:type_1, :type_2, :type_3, :custom] 
end 

ProductCapture型号:

class ProductCapture < ActiveRecord::Base 
    belongs_to :product 
    attr_accessible :base_price, :margin 

    def price 
    price + margin 
    end 
end 

....other models

这样的想法是:

  1. 用户可以有很多的零售商或供应商(验证要求对于这一点,我不知道这是正确的或不是在你的情况下)
  2. 我们随时为零售商和供应商订购lat确保最新的价格将被应用
  3. 当零售商更新他们的价格(基准价格+保证金)时,我们创建一个新的ProductCapture成为最新的一个,所有旧的捕获仍然在数据库中,因为旧的订单仍在使用它。
+1

谢谢。这正是我需要的。感谢您花时间解释它。供应商也更新零售商的价格。 – suyesh