我在设计一个简单的订购系统时遇到问题,客户可以通过选择商品来创建订单。
我想要的模型是:Customer,Order,OrderLine和Item。 客户创建一个包含许多OrderLines的订单,每个OrderLine包含一个具有数量,sell_price等的项目。项目是"产品"客户可以选择。选择项目并分配所需数量后,将创建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
也许订单还应包含&#34; belongs_to:customer&#34;?
以下是架构文件的相关部分供参考:
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"
答案 0 :(得分:0)
我认为您当前的架构没有任何问题。
也许Order也应该包含“belongs_to:customer”?
是的,这可能会派上用场。您可以随时在需要时添加它。
编辑:
t.decimal "unit_price", precision: 5, scale: 2
t.decimal "total_price", precision: 7, scale: 2
为什么需要在OrderLines中保存价格?您已在商品模型中拥有商品价格。