Can a nested set have duplicate child objects or multiple 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开发者_JAVA百科 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!!!
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)
精彩评论