首页 > 解决方案 > 我的市场轨道项目的两个数据库模式之间的困境

问题描述

当用户可以买卖时,我正在建立一个市场。当然,他自己的产品是买不到的,一旦他的一个产品被买了,后面就会有一个数量逻辑控制器。然而。我仍然坚持对模式进行建模。

目前我正在使用这个模式。

  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

所以基本上我要在表 Orders 和 Products 之间使用 HABTOM (has_and_belongs_to_many) 关联,正如你在我的模型中看到的那样,因为一个订单可以有很多产品,一个产品可以有很多订单(这里我不确定xD,我想我错了)。无论如何,我的困境是这样的,因为我在互联网上读到,在大多数情况下,在这种情况下,您可以使用 has_many :through 关联(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 :through 关联?谢谢

标签: ruby-on-railsruby

解决方案


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

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

您需要“卖方”和“买方”(或您选择的任何名称)的概念。

像这样的东西:

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表)。但是您仍然需要明确区分“卖家”和“买家”。


推荐阅读