Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Postgres和Ruby——有没有更好的方法来构建我的数据库?_Ruby On Rails - Fatal编程技术网

Ruby on rails Postgres和Ruby——有没有更好的方法来构建我的数据库?

Ruby on rails Postgres和Ruby——有没有更好的方法来构建我的数据库?,ruby-on-rails,Ruby On Rails,我现在正在计划我的db。在把我的ERD放在一起的过程中,我很快注意到所有的表都与我的商店表和我的amazon_凭证表相关。是否有更好/更有效的设计或关系可以让我的数据库更高效、更易于使用 还有一个警告,我不想在所有查询中都必须通过我的amazon\u凭证表。例如我希望能够同时做这两件事。查找(1)。装箱单,为一家商店获取所有装箱单,以及能够做这件事购物。查找(1)。亚马逊凭证。装箱单获取特定市场的装箱单 考虑到这一点,我几乎是在使用我的amazon\u凭证表作为marketplace的替身,因

我现在正在计划我的db。在把我的ERD放在一起的过程中,我很快注意到所有的表都与我的
商店
表和我的
amazon_凭证
表相关。是否有更好/更有效的设计或关系可以让我的数据库更高效、更易于使用

还有一个警告,我不想在所有查询中都必须通过我的
amazon\u凭证
表。例如我希望能够同时做这两件事。查找(1)。装箱单,为一家
商店
获取所有
装箱单
,以及能够做这件事
购物。查找(1)。亚马逊凭证。装箱单
获取特定市场的
装箱单

考虑到这一点,我几乎是在使用我的
amazon\u凭证
表作为
marketplace
的替身,因为凭证的范围固有于
marketplace


这是一个好的设计还是有更好的方法?

与分散在数据库中的商店和AmazonCredentials的重复关系可以替换为间接关系。幸运的是,Rails提供了使间接关系看起来直接的方法:

你不需要第一次就把它做得完美,也不需要预测每一个可能的用途。帮助您安全地更改设计以满足不断变化的需求

让我们建立基本的关系。我做了一些假设,比如运输方式与市场挂钩

  • 产品具有关于自身的信息

  • 商店有产品、营销方式(市场)、这些市场的凭证和订单

  • 订单包括谁下的订单、从哪个商店、通过哪个市场、订购了什么产品、以什么价格订购了多少产品、如何发货以及如何跟踪

  • 市场有多种运输方式

我们马上遇到了一个问题:凭证。商店需要每个市场的凭证。这可以通过一个中间模型来解决:MarketplaceAccount,它将商店和具有商店凭证的市场结合起来。可以将凭据制作成键/值对的行

订单需要记住关于订购了哪些产品、订购了多少以及订购多少的详细信息。这需要另一个中间模型:ProductOrder

基本模型如下所示:

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提供了使间接关系看起来直接的方法:

你不需要第一次就把它做得完美,也不需要预测每一个可能的用途。帮助您安全地更改设计以满足不断变化的需求

让我们建立基本的关系。我做了一些假设,比如运输方式与市场挂钩

  • 产品具有关于自身的信息

  • 商店有产品、营销方式(市场)、这些市场的凭证和订单

  • 订单包括谁下的订单、从哪个商店、通过哪个市场、订购了什么产品、以什么价格订购了多少产品、如何发货以及如何跟踪

  • 市场有多种运输方式

我们马上遇到了一个问题:凭证。商店需要每个市场的凭证。这可以通过一个中间模型来解决:MarketplaceAccount,它将商店和具有商店凭证的市场结合起来。可以将凭据制作成键/值对的行

订单需要记住关于订购了哪些产品、订购了多少以及订购多少的详细信息。这需要另一个中间模型:ProductOrder

基本模型如下所示:

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
涵盖特定国家/地区。并根据客户地址使用正确的凭据集..您可以