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