我的市场轨道项目的两个数据库模式之间的困境



我正在建立一个用户可以买卖的市场。 当然,他将无法购买自己的产品,一旦购买了他的产品之一,后面就会有一个数量逻辑控制器。然而。我仍然坚持对架构进行建模。

目前我正在使用这个架构。

ActiveRecord::Schema.define(version: 2018_09_11_202223) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "orders", force: :cascade do |t|
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_orders_on_user_id"
end
create_table "orders_products", id: false, force: :cascade do |t|
t.integer "order_id", null: false
t.integer "product_id", null: false
t.index ["order_id", "product_id"], name: "index_orders_products_on_order_id_and_product_id"
t.index ["product_id", "order_id"], name: "index_orders_products_on_product_id_and_order_id"
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "tagline"
t.integer "user_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "price"
t.text "description"
t.index ["category_id"], name: "index_products_on_category_id"
t.index ["user_id"], name: "index_products_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end

这些模型:

class Category < ApplicationRecord
has_many :products
end
class Order < ApplicationRecord
has_and_belongs_to_many :products
belongs_to :user
end
class Product < ApplicationRecord
has_and_belongs_to_many :orders
belongs_to :category
belongs_to :user
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :orders, :dependent => :destroy
has_many :products, :dependent => :destroy
end

所以基本上我将在订单和产品表之间使用 HABTOM (has_and_belongs_to_many( 关联,正如您在我的模型中看到的那样,因为一个订单可以有很多产品,一个产品可以有很多订单(这里我不确定 xD,我想我错了(。无论如何,我的困境是这样的,因为我在互联网上读到,在大多数情况下,在这种情况下,您可以使用has_many:通过关联(HMTA(,如下所示:

class Category < ApplicationRecord
has_many :products
end
class User
has_many :orders
has_many :products, through: :orders
end
class Order
belongs_to :user
belongs_to :product
end
class Product
belongs_to :category
has_many :orders
has_many :users, through: :orders
end

请记住,在我的市场中,我希望人们可以购买和出售,这就是为什么用户可以拥有许多产品,但产品必须是唯一的,并且应该只属于一个特定的用户,例如一辆旧车,不存在两辆完全相同的汽车,这就是为什么我在我的产品模型中使用这种关联:

class Product < ApplicationRecord
has_and_belongs_to_many :orders
belongs_to :category
belongs_to :user
end

所以最后你建议我怎么做? 我应该从HABTOM协会切换到HMTA吗?在这种情况下,假设我想销售产品,如何管理用户和产品关联?是否可以仅使用 has_many :通过关联?谢谢

产品必须是唯一的,并且只能属于一个特定用户

关键是products表和users表之间需要有两个链接。

你需要"卖家"和"买家"的概念(或者你选择的任何名称(。

像这样:

class Product < ApplicationRecord
belongs_to :seller, class_name: "User"
has_many :buyers, through: :orders, class_name: "User"
end
class User
has_many :orders
has_many :products_bought, through: :orders, class_name: 'Product'
has_many :products_sold, class_name: 'Product'
end

然后,您可以对产品添加唯一性验证检查(可能限定为seller_id

如果一个产品有多个卖家,那么您可以稍微调整此关联以使用更通用的连接表(即重命名orders表(。但是您仍然需要明确区分"卖方"和"买方"。

最新更新