ここで必要なのは、多対多の関係をモデル化するための関連クラスです。そこ(例えば、ツリー型構造を使用して)、これをモデル化する他の方法がありますが、「設定」は、あなたが望むものであるならば、これは方法Iである
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)
:これが完了すると
、あなたのようなことを行うことができます