我理解你的感受,因爲我之前有完全相同這個問題,讓我分享我做什麼,希望它可以幫助您解決問題:
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
這樣的想法是:
- 用戶可以有很多的零售商或供應商(驗證要求對於這一點,我不知道這是正確的或不是在你的情況下)
- 我們隨時爲零售商和供應商訂購lat確保最新的價格將被應用
- 當零售商更新他們的價格(基準價格+保證金)時,我們創建一個新的
ProductCapture
成爲最新的一個,所有舊的捕獲仍然在數據庫中,因爲舊的訂單仍在使用它。
需要一些時間並從頭到尾完成[Rails Guides](http://guides.rubyonrails.org/getting_started.html)並執行給出的所有示例。 –
歡迎來到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 –