【问题标题】:Rails modeling help, association and logicRails 建模帮助、关联和逻辑
【发布时间】:2016-01-21 15:56:40
【问题描述】:

我是一名初级开发人员,刚刚完成了第一个 Rails 项目。我面临许多障碍,但从中学到了很多东西。到目前为止,我能够找出Devise 中的多个用户以及所有这些好东西。但对授权半信半疑。我还在学习,我相信我会弄明白的。

但过去一周我唯一的砖墙时刻是为我的应用程序建模以获取订单部分。这是我正在开发的应用程序的一个小总结:

  1. B2B
  2. 用户类型为零售商和供应商
  3. 零售商向供应商下订单
  4. 只有一种产品具有 3 种或更多不同类型,product_type1、product_type2、product_type3
  5. 供应商每天更新产品类型的价格,零售商在他们的仪表板中看到当前价格
  6. 供应商还为每个零售商制定了每种产品价格的公式。

例如,他的底价+利润,每个零售商的利润是不同的。

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

我需要什么?

产品型号?价格和类型?

单独的公式模型?

【问题讨论】:

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


【解决方案1】:

我理解你的感受,因为我之前也遇到过这个问题,分享一下我的做法,希望对解决你的问题有帮助:

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. 我们总是为零售商和供应商创建一个带有最新产品捕获的订单,这样可以确保应用最新价格
  3. 当零售商更新他们的价格(基本价格 + 保证金)时,我们会创建一个新的 ProductCapture,它会成为最新的,所有旧捕获仍在数据库中,因为旧订单仍在使用它。

【讨论】:

  • 谢谢。这正是我所需要的。感谢您花时间解释它。供应商也会更新零售商的价格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多