2016-01-21 83 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