嵌套集合可以有重复的子对象或多个parent_id/root/nodes吗?

发布于 2024-10-18 04:02:28 字数 139 浏览 3 评论 0原文

嵌套集合可以有重复的子对象或多个parent_id/root/nodes吗?

例如,我想创建一个可以管理零件和设备的应用程序。然而,特定设备也可以具有与其他设备相同的部件。

关于最好的方法有什么想法吗?

谢谢你!!!

Can a nested set have duplicate child objects or multiple parent_id/root/nodes?

For instance, I want to create an application that can manage parts and equipment. However, a specific equipment can have the same parts from other equipment as well.

Any thoughts on the best approach for this?

Thank you!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-10-25 04:02:28

我认为您这里需要的是一个关联类来帮助建模多对多关系。在 Rails 中,这可能看起来像这样:

class Equipment < ActiveRecord::Base
  has_many :part_relationships
  has_many :parts, :through => :part_relationships
end

class Part < ActiveRecord::Base
  has_many :part_relationships
  has_many :equipment, :through => :part_relationships
end

class PartRelationship < ActiveRecord::Base
  belongs_to :equipment
  belongs_to :part
end

还有其他对此进行建模的方法(例如使用树类型结构),但如果“集合”是您想要的,那么这就是我要走的方式。

完成此操作后,您可以执行以下操作:

e = Equipment.find(:first)
e.parts # Returns all the parts on this equipment, including shared

p = Part.find(:first)
p.equipment # Returns all equipment this part features in.

# Create a new relationship between e and p
PartRelationship.create(:equipment => e, :part => p)

I think what you need here is an association class to help model the many-to-many relationship. In rails, this might look something like this:

class Equipment < ActiveRecord::Base
  has_many :part_relationships
  has_many :parts, :through => :part_relationships
end

class Part < ActiveRecord::Base
  has_many :part_relationships
  has_many :equipment, :through => :part_relationships
end

class PartRelationship < ActiveRecord::Base
  belongs_to :equipment
  belongs_to :part
end

There are other ways of modelling this (e.g. using a tree type structure), but if a 'set' is what you want, then this is the way I'd go.

Once this is done, you can do things like:

e = Equipment.find(:first)
e.parts # Returns all the parts on this equipment, including shared

p = Part.find(:first)
p.equipment # Returns all equipment this part features in.

# Create a new relationship between e and p
PartRelationship.create(:equipment => e, :part => p)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文