【发布时间】: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