Ruby on rails 我的MarketplaceRails项目的两种数据库模式之间的两难选择
我正在建立一个市场,用户可以买卖。 当然,他将无法购买自己的产品,一旦他的一个产品被购买,后面将有一个数量逻辑控制器。然而。我仍然在为模式建模 目前我正在使用这个模式Ruby on rails 我的MarketplaceRails项目的两种数据库模式之间的两难选择,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在建立一个市场,用户可以买卖。 当然,他将无法购买自己的产品,一旦他的一个产品被购买,后面将有一个数量逻辑控制器。然而。我仍然在为模式建模 目前我正在使用这个模式 ActiveRecord::Schema.define(version: 2018_09_11_202223) do create_table "categories", force: :cascade do |t| t.string "name" t.datetime "created_at", nul
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_和_属于_-many)关联,正如您在我的模型中看到的,因为一个订单可以有很多产品,一个产品可以有很多订单(这里我不确定xD,我认为我错了)。无论如何,我的困境是这样的,因为我在互联网上读到,在大多数情况下,在这种情况下,你可以使用has_many:through association(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
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
类别
请记住,在我的市场中,我希望人们可以买卖,这就是为什么一个用户可以有许多产品,但产品必须是唯一的,并且应该只属于一个特定的用户,例如一辆旧车,并不存在两辆完全相同的车,这就是为什么我在我的产品模型中使用此关联:
class Product < ApplicationRecord
has_and_belongs_to_many :orders
belongs_to :category
belongs_to :user
end
类产品
那么最后你建议我做什么?我应该从HABTOM协会转到HMTA吗?在这种情况下,假设我想销售一种产品,如何管理用户和产品关联?仅仅通过关联使用has\u many:是否可能?多谢各位
产品必须是唯一的,并且只能属于一个特定用户
关键是产品
表和用户
表之间需要有两个链接
你需要一个“卖方”和“买方”的概念(或者你选择的任何名称)
大概是这样的:
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
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
类产品
然后,您可以在产品上添加唯一性验证检查(可能范围为卖方id
?)
如果一个产品有多个销售商,那么您可以稍微调整此关联以使用更通用的联接表(即重命名
orders
表)。但是,您仍然需要明确区分“卖方”和“买方”。在定义这样的自定义关联时,还应该在关联上定义一个反向的,以便Rails知道如何最大限度地减少在访问对象之间的数据时进行的数据库查询。