Rails 中的订单处理 - 建模和创建关联的最佳方式是什么?

Order processing in Rails - what is the best way to model this and create the associations?

我在为客户可以通过选择商品创建订单的简单订购系统建模时遇到问题。

我想要的模型是:Customer、Order、OrderLine 和 Item。 客户创建一个包含许多 OrderLine 的订单,每个 OrderLine 包含一个带有数量 selling_price 等的项目。客户可以从 "products" 中选择项目。一旦选择了一个项目并分配了所需的数量,就会创建一个 OrderLine。

鉴于此,协会的最佳设置是什么?

以下是当前状态下的模型:

class Customer < ActiveRecord::Base
   has_many :orders
end

class Item < ActiveRecord::Base
   has_many :order_lines
   has_many :orders, through: :order_lines
end

class OrderLine < ActiveRecord::Base
  belongs_to :order
  belongs_to :item
end

class Order < ActiveRecord::Base
  has_many :order_lines
  has_many :items, through: :order_lines
end

也许订单还应该包含 "belongs_to :customer"?

以下是架构文件的相关部分供参考:

create_table "customers", force: :cascade do |t|
  t.string   "fname"
  t.string   "middle_initial"
  t.string   "lname"
  t.string   "street_add"
  t.string   "city"
  t.string   "state"
  t.string   "zipcode"
  t.string   "email"
  t.string   "home_phone"
  t.string   "cell_phone"
  t.string   "phone_pref"
  t.string   "password"
  t.datetime "created_at",     null: false
  t.datetime "updated_at",     null: false
end

create_table "items", force: :cascade do |t|
  t.integer  "barcode"
  t.text     "description"
  t.decimal  "selling_price",   precision: 5, scale: 2
  t.string   "unit_of_measure"
  t.integer  "qty_on_hand"
  t.integer  "location_aisle"
  t.string   "location_area"
  t.datetime "created_at",                              null: false
  t.datetime "updated_at",                              null: false
end

create_table "order_lines", force: :cascade do |t|
  t.integer  "qty_ordered"
  t.decimal  "unit_price",        precision: 5, scale: 2
  t.decimal  "total_price",       precision: 7, scale: 2
  t.integer  "order_id"
  t.integer  "item_id"
  t.datetime "created_at",                                null: false
  t.datetime "updated_at",                                null: false
end

add_index "order_lines", ["item_id"], name: "index_order_lines_on_item_id"
add_index "order_lines", ["order_id"], name: "index_order_lines_on_order_id"

create_table "orders", force: :cascade do |t|
  t.string   "billing_street_add"
  t.string   "billing_city"
  t.string   "billing_state"
  t.string   "billing_zipcode"
  t.string   "shipping_street_address"
  t.string   "shipping_city"
  t.string   "shipping_state"
  t.string   "shipping_zipcode"
  t.string   "cc_fname"
  t.string   "cc_middle_initial"
  t.string   "cc_lname"
  t.string   "cc_number"
  t.string   "cc_security_code"
  t.string   "cc_exp_month"
  t.string   "cc_exp_year"
  t.decimal  "subtotal",                precision: 7,  scale: 2
  t.integer  "tax_percent"
  t.decimal  "shipping_fee",            precision: 5,  scale: 2
  t.decimal  "total",                   precision: 20, scale: 2
  t.integer  "customer_id"
  t.integer  "order_line_id"
  t.datetime "created_at",                                       null: false
  t.datetime "updated_at",                                       null: false
end

add_index "orders", ["customer_id"], name: "index_orders_on_customer_id"
add_index "orders", ["order_line_id"], name: "index_orders_on_order_line_id"

我没有发现您当前的架构有任何问题。

Perhaps Order should also contain "belongs_to :customer"?

是的,这可能会派上用场。您可以随时添加它。

编辑:

    t.decimal  "unit_price",        precision: 5, scale: 2
    t.decimal  "total_price",       precision: 7, scale: 2

为什么需要在 OrderLines 中保存价格?您的项目模型中已有项目价格。