【问题标题】:Rails associations between clients and sellers - has_many :through or has_and_belongs_to_many客户和卖家之间的 Rails 关联 - has_many :through 或 has_and_belongs_to_many
【发布时间】:2014-05-02 10:36:23
【问题描述】:

我正在开始一个带有 产品客户卖家的 Rails 项目。每个卖家has_many products。每个客户has_many products。 (就我而言,每个客户一次只购买一种产品)。

我想知道谁是我的客户的卖家和我的卖家的客户,我知道,他们将通过购买一种产品联系起来。

我应该在客户和卖家之间使用has_and_belongs_to_many 关联吗?或者双 has_many through :products,比如:

卖家

has_many :clients through :products

Belongs_to :products

客户

has_many :sellers through :products

Belongs_to :products

为了避免product 类中有两个belongs_to,这样可以吗?

class Client < ActiveRecord::Base

has_many :products, as: :productable

has_many :sellers, through: :products

end

class Seller < ActiveRecord::Base

has_many :products, as: :productable

has_many :clients, through: :products

end

class Product < ActiveRecord::Base

belongs_to :productable, polymorphic: true

end

提前感谢您的回答。

【问题讨论】:

  • 首先,在这两个模型和 Product 中都应该是 has_and_belongs_to_many :products。因为它是多对多的关系 b/w 客户-产品,卖家-产品。
  • 事实上,每个产品都是独一无二的。就好像他们是画,卖家是画家。每个画家可以创作/创作多幅画,但一幅画只有一个画家,而且只有一个客户。但是客户可以从画家那里购买许多画作。

标签: ruby-on-rails activerecord associations has-many-through


【解决方案1】:

我会在这里选择 has_many :through

class Client < ActiveRecord::Base

has_many :products

has_many :sellers, through: :products

end

class Seller < ActiveRecord::Base

has_many :prodcuts

has_many :clients, through: :products

end

class Product < ActiveRecord::Base

belongs_to :client
belongs_to :seller

end

最简单的经验法则是您应该设置一个 has_many :through relationship 如果您需要使用关系模型 作为一个独立的实体。如果您不需要对 关系模型,建立一个关系模型可能更简单 has_and_belongs_to_many 关系(尽管你需要记住 在数据库中创建连接表)。

如果你需要验证、回调,你应该使用 has_many :through 或连接模型上的额外属性。

另请参阅这些 Guides 以在 HABTMhas_many :through

之间进行选择

【讨论】:

  • 我会总是推荐has_many :through而不是habtm。它允许您使用额外的信息填充连接模型,这些信息通常会在以后需要。
  • 非常感谢。除此之外,我想知道是否最好避免产品类中的两个 belongs_to 并使用多态关联。
  • @Francky 是的,Polymorphic 看起来不错。但是使用时要小心。这是一个棘手的问题。
  • @MaxWilliams 同意!我也喜欢同样的:)
  • @Francky 最后,如果您认为我的回答对您有帮助,请不要忘记接受 :)
【解决方案2】:

我想从另一端解决您的问题:让我们从产品开始。我想这会澄清很多事情。

所以你有三个模型:Seller, ClientProduct

Product 有一个卖家和一个客户。在您的模型中会这样:

class Product
  belongs_to :seller
  belongs_to :client
end

这意味着在 products 表中我们有一个列 seller_idclient_id

Afaik 产品始终需要两者兼有。所以这也意味着你不能在这里使用多态关联。至少不是你提出的方式。如果你写

 belongs_to :productable, polymorphic: true

您将添加字段productable_id 和productable_typeto yourProduct` 模型。但这只是 1 链接(所以要么是卖家要么是客户,但绝不是两者兼而有之)。您可以在此处引入链接表,因此产品可以链接到许多“产品”,但在您的情况下,我认为这不是重点。你知道一个产品有一个卖家和一个客户。

其次,现在已经建立了,您的Product 正是客户和卖家之间的链接表。因此,您不必引入新的链接表,只需使用已有的链接表即可。

class Seller
  has_many :products
  has_many :clients, through: :products
end

class Client
  has_many :products
  has_many :sellers, through: :products
end

总之:

  • 使用has_many :through,因为您已经将链接表作为模型。如果您不关心连接表(链接表),请仅使用 habtm。
  • 您不能在此处使用多态关联,因为您需要两个链接(不引入链接表,恕我直言,这似乎有点过头了)。我喜欢具有显式 seller_idclient_id 的明确性、清晰性和可读性,而且它也更易于管理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多