Ruby on rails Postgres和Ruby——有没有更好的方法来构建我的数据库?
我现在正在计划我的db。在把我的ERD放在一起的过程中,我很快注意到所有的表都与我的Ruby on rails Postgres和Ruby——有没有更好的方法来构建我的数据库?,ruby-on-rails,Ruby On Rails,我现在正在计划我的db。在把我的ERD放在一起的过程中,我很快注意到所有的表都与我的商店表和我的amazon_凭证表相关。是否有更好/更有效的设计或关系可以让我的数据库更高效、更易于使用 还有一个警告,我不想在所有查询中都必须通过我的amazon\u凭证表。例如我希望能够同时做这两件事。查找(1)。装箱单,为一家商店获取所有装箱单,以及能够做这件事购物。查找(1)。亚马逊凭证。装箱单获取特定市场的装箱单 考虑到这一点,我几乎是在使用我的amazon\u凭证表作为marketplace的替身,因
商店
表和我的amazon_凭证
表相关。是否有更好/更有效的设计或关系可以让我的数据库更高效、更易于使用
还有一个警告,我不想在所有查询中都必须通过我的amazon\u凭证
表。例如我希望能够同时做这两件事。查找(1)。装箱单,为一家商店
获取所有装箱单
,以及能够做这件事购物。查找(1)。亚马逊凭证。装箱单
获取特定市场的装箱单
考虑到这一点,我几乎是在使用我的amazon\u凭证
表作为marketplace
的替身,因为凭证的范围固有于marketplace
这是一个好的设计还是有更好的方法?与分散在数据库中的商店和AmazonCredentials的重复关系可以替换为间接关系。幸运的是,Rails提供了使间接关系看起来直接的方法: 你不需要第一次就把它做得完美,也不需要预测每一个可能的用途。帮助您安全地更改设计以满足不断变化的需求 让我们建立基本的关系。我做了一些假设,比如运输方式与市场挂钩
- 产品具有关于自身的信息
- 商店有产品、营销方式(市场)、这些市场的凭证和订单
- 订单包括谁下的订单、从哪个商店、通过哪个市场、订购了什么产品、以什么价格订购了多少产品、如何发货以及如何跟踪
- 市场有多种运输方式
class Product < ApplicationRecord
belongs_to :shop
has_many :product_orders
has_many :orders, through: :product_orders
has_many :customers, through: :orders
# name
# description
# sku
end
class Shop < ApplicationRecord
has_many :products
has_many :marketplace_accounts
has_many :marketplaces, through: :marketplace_accounts
has_many :orders
has_many :customers, through: :orders
# name
# country
end
class Order < ApplicationRecord
belongs_to :customer
# Remember which MarketplaceAccount this was done with
belongs_to :marketplace_account
has_one :shop, through: :marketplace_account
has_one :marketplace, through: :marketplace_account
# How is it being shipped?
belongs_to :shipping_method
has_many :product_orders
has_many :products, through: :product_orders
# tracking number
# status
end
class ProductOrder < ApplicationRecord
belongs_to :order
belongs_to :product
# price
# quantity
end
# Holds the Shop's credentials for a Marketplace.
class MarketplaceAccount < ApplicationRecord
belongs_to :shop
belongs_to :marketplace
has_many :credentials
end
# Each row is simply a key and value.
# Consider encrypting this table.
class Credentials < ApplicationRecord
belongs_to :marketplace_account
# key
# value
end
class Marketplace < ApplicationRecord
has_many :shipping_methods
has_many :marketplace_accounts
has_many :shops, through: :marketplace_accounts
# name
end
class ShippingMethod < ApplicationRecord
belongs_to :marketplace
has_many :shops, through: :marketplace
# speed
# rate
# name
end
class Customer < ApplicationRecord
has_many :orders
has_many :product_orders, through: :orders
has_many :products, through: :product_orders
end
大多数事情都与商店产品而不是产品有关
你有一家使用单一货币的商店。该设计可以扩展到允许商店使用多种货币
class Currency < ApplicationRecord
# symbol
# conversion rate
end
class ShopCurrencies < ApplicationRecord
belongs_to :shop
belongs_to :currency
end
class Shop < ApplicationRecord
has_many :shop_currencies
has_many :currencies, through: :shop_currencies
end
class Currency
有很多:货币,通过::shop\u货币
让购物。货币
起作用
这是最基本的。有了这些,你就可以根据自己的需要来设计基本的布局。与分散在数据库中的商店和AmazonCredentials的重复关系可以被间接关系所取代。幸运的是,Rails提供了使间接关系看起来直接的方法: 你不需要第一次就把它做得完美,也不需要预测每一个可能的用途。帮助您安全地更改设计以满足不断变化的需求 让我们建立基本的关系。我做了一些假设,比如运输方式与市场挂钩
- 产品具有关于自身的信息
- 商店有产品、营销方式(市场)、这些市场的凭证和订单
- 订单包括谁下的订单、从哪个商店、通过哪个市场、订购了什么产品、以什么价格订购了多少产品、如何发货以及如何跟踪
- 市场有多种运输方式
class Product < ApplicationRecord
belongs_to :shop
has_many :product_orders
has_many :orders, through: :product_orders
has_many :customers, through: :orders
# name
# description
# sku
end
class Shop < ApplicationRecord
has_many :products
has_many :marketplace_accounts
has_many :marketplaces, through: :marketplace_accounts
has_many :orders
has_many :customers, through: :orders
# name
# country
end
class Order < ApplicationRecord
belongs_to :customer
# Remember which MarketplaceAccount this was done with
belongs_to :marketplace_account
has_one :shop, through: :marketplace_account
has_one :marketplace, through: :marketplace_account
# How is it being shipped?
belongs_to :shipping_method
has_many :product_orders
has_many :products, through: :product_orders
# tracking number
# status
end
class ProductOrder < ApplicationRecord
belongs_to :order
belongs_to :product
# price
# quantity
end
# Holds the Shop's credentials for a Marketplace.
class MarketplaceAccount < ApplicationRecord
belongs_to :shop
belongs_to :marketplace
has_many :credentials
end
# Each row is simply a key and value.
# Consider encrypting this table.
class Credentials < ApplicationRecord
belongs_to :marketplace_account
# key
# value
end
class Marketplace < ApplicationRecord
has_many :shipping_methods
has_many :marketplace_accounts
has_many :shops, through: :marketplace_accounts
# name
end
class ShippingMethod < ApplicationRecord
belongs_to :marketplace
has_many :shops, through: :marketplace
# speed
# rate
# name
end
class Customer < ApplicationRecord
has_many :orders
has_many :product_orders, through: :orders
has_many :products, through: :product_orders
end
大多数事情都与商店产品而不是产品有关
你有一家使用单一货币的商店。该设计可以扩展到允许商店使用多种货币
class Currency < ApplicationRecord
# symbol
# conversion rate
end
class ShopCurrencies < ApplicationRecord
belongs_to :shop
belongs_to :currency
end
class Shop < ApplicationRecord
has_many :shop_currencies
has_many :currencies, through: :shop_currencies
end
class Currency
有很多:货币,通过::shop\u货币
让购物。货币
起作用
这是最基本的。有了它,你就可以根据自己的需要调整基本布局。你有了它,商店就可以拥有0个或更多的亚马逊凭证。这就是你想要的吗?你在用Rails吗?“我不想在我的amazon_凭证表中查找所有查询”为什么不?@Schwern hi。。是的,使用Rails。。是的,
商店
可以拥有0个或更多的亚马逊认证
如果一家商店可以有很多亚马逊认证,你如何知道该使用哪一个?@Schwern它基于订单
中的客户地址。amazon\u凭证中的marketplace
涵盖特定国家/地区。并根据客户地址使用正确的凭据集..您可以