在不同对象中存储不同对象 id_s 的多对多关系
many to many relation that store different objects id_s in differen object
嗨,我想在 Rails 中创建关系。
我有战士模型、法师模型和阵营模型。
我想创建这样的关系:
warrior model can have_many factions
mage model can have many factions.
Faction model can have many warriors and mages
我如何创建战士和法师对象与派系对象之间的关系,这将存储属于特定 faction/factions 的战士和法师的 id_s?
所以当我打电话时:
faction.warriors I get warriors of specific faction.
faction.mage I get mages of this faction
warriors.faction I get the warrior faction.
mage.faction I get the mage faction.
我在考虑多态关联。但它只有一个主人。
有线索吗?
How can I create relation between warriors and mages objects and faction object, that will store id_s of both warriors and mages that belong to specific faction/factions ?
Warrior
has_and_belongs_to_many :factions
Mage
has_and_belongs_to_many :factions
Faction
has_and_belongs_to_many :mages
has_and_belongs_to_many :warriors
不太清楚你想要 many_to_many 还是 has_many 关系。
但是从你在这里写的内容来看:
faction.warriors I get warriors of specific faction.
faction.mage I get mages of this faction
warriors.faction I get the warrior faction.
mage.faction I get the mage faction.
看来你只需要一个简单的关联。如果那是正确的,你的 类 应该是这样的:
class Warrior < ActiveRecord::Base
belongs_to :faction
end
class Mage < ActiveRecord::Base
belongs_to :faction
end
class Faction < ActiveRecord::Base
has_many :warriors
has_many :mages
end
干杯!
嗨,我想在 Rails 中创建关系。
我有战士模型、法师模型和阵营模型。
我想创建这样的关系:
warrior model can have_many factions
mage model can have many factions.
Faction model can have many warriors and mages
我如何创建战士和法师对象与派系对象之间的关系,这将存储属于特定 faction/factions 的战士和法师的 id_s?
所以当我打电话时:
faction.warriors I get warriors of specific faction.
faction.mage I get mages of this faction
warriors.faction I get the warrior faction.
mage.faction I get the mage faction.
我在考虑多态关联。但它只有一个主人。
有线索吗?
How can I create relation between warriors and mages objects and faction object, that will store id_s of both warriors and mages that belong to specific faction/factions ?
Warrior
has_and_belongs_to_many :factions
Mage
has_and_belongs_to_many :factions
Faction
has_and_belongs_to_many :mages
has_and_belongs_to_many :warriors
不太清楚你想要 many_to_many 还是 has_many 关系。
但是从你在这里写的内容来看:
faction.warriors I get warriors of specific faction. faction.mage I get mages of this faction warriors.faction I get the warrior faction. mage.faction I get the mage faction.
看来你只需要一个简单的关联。如果那是正确的,你的 类 应该是这样的:
class Warrior < ActiveRecord::Base
belongs_to :faction
end
class Mage < ActiveRecord::Base
belongs_to :faction
end
class Faction < ActiveRecord::Base
has_many :warriors
has_many :mages
end
干杯!