【问题标题】:Order processing in Rails - what is the best way to model this and create the associations?Rails 中的订单处理 - 对此进行建模和创建关联的最佳方式是什么?
【发布时间】:2015-11-15 17:51:39
【问题描述】:

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

我想要的模型是:Customer、Order、OrderLine 和 Item。 客户创建一个包含许多 OrderLine 的订单,每个 OrderLine 包含一个具有数量、销售价格等的项目。项目是客户可以选择的“产品”。一旦选择了一个项目并分配了所需的数量,就会创建一个 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"

【问题讨论】:

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


    【解决方案1】:

    我认为您当前的架构没有任何问题。

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

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

    编辑:

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

    为什么需要在 OrderLines 中保存价格?您的商品模型中已经有了商品价格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多