Ruby on Rails ߝ具有多个多对多关联的模型
我目前正在开发《地下城与地下城》 Ruby on Rails 3.2.1 中的 Dragons 第四版角色表应用程序。我目前正处于一个阶段,我意识到我需要将角色种族(矮人、精灵……)与能力奖励联系起来。 (例如:作为种族特征,矮人的体质和智慧能力获得+2加值。)
我目前的设置如下:
class Character < ActiveRecord::Base {
has_many :attributions
has_many :abilities, through: :attributions
}
class Attribution < ActiveRecord::Base {
belongs_to :character
belongs_to :ability
# The "attributions" table also has the column "score,"
# which is the character's ability score
}
class Ability < ActiveRecord::Base {
has_many :attributions
has_many :characters, through: :attributions
}
如您所见,每个角色都有自己的一套能力。我想我可能会对角色竞赛做同样的事情,但我不确定这个领域是否有任何最佳实践。我可能可以使用相同的连接模型,创建类似的东西(Character
将保持不变,除了覆盖的 powered
方法,也许):
class CharacterRace < ActiveRecord::Base {
# I am not sure if this will actually work, but I hope
# you understand what I am trying to do with this
has_many :racial_ability_traits, class_name: "Attribution"
has_many :abilities, through: :racial_ability_traits
}
class Attribution < ActiveRecord::Base {
# The following two are just like before
belongs_to :character
belongs_to :ability
# This field would be new
belongs_to :character_class
}
class Ability < ActiveRecord::Base {
# These are the same as above
has_many :attributions
has_many :characters, through: :attributions
# These would be new, and I am not sure if it will work, but
# I hope you understand what I amm trying to do with this
has_many :racial_traits, class_name: "Attribution"
has_many :character_races, through: :racial_ability_traits
}
但是,即使 如果这可行,我不知何故觉得对不同的连接使用相同的连接模型(即使目的几乎相同)是一种丑陋的方法。当然,我可以创建一种不同类型的连接,如下所示:
class CharacterRace < ActiveRecord::Base {
has_many :abilities, through: :attributions
# These are for the racial bonus
has_many :racial_ability_bonuses
has_many :abilities, through: :racial_ability_bonuses
}
class RacialAbilityBonus < ActiveRecord::Base {
belongs_to :character_race
belongs_to :ability
}
class Ability < ActiveRecord::Base {
has_many :attributions
has_many :abilities, through: :attributions
# These are for the racial bonus
has_many :racial_ability_bonuses
has_many :character_races, through: :racial_ability_bonuses
}
这可能会起作用,但有一个问题。不仅种族可以拥有这样的特征/奖励。魔法物品也可能带来能力加值。因此,Item
模型还应该通过: :item_ability_bonus 关联赋予一个 has_many :ability。继续沿着上面解释的道路前进,这将导致很多连接模型。
因此我想问一下你们是否知道处理这个问题的好方法。也欢迎任何关于改进我现有代码的建议。
我非常感谢你们所有认真的回答。 :-)
I am currently developing a Dungeons & Dragons 4th Edition Character Sheet application in Ruby on Rails 3.2.1. I am currently at a stage where I realize that I need to associate character races (dwarves, elves, …) with ability bonuses. (Example: a dwarf gets +2 bonuses to the Constitution and Wisdom abilities as a racial trait.)
I currently have the following set up:
class Character < ActiveRecord::Base {
has_many :attributions
has_many :abilities, through: :attributions
}
class Attribution < ActiveRecord::Base {
belongs_to :character
belongs_to :ability
# The "attributions" table also has the column "score,"
# which is the character's ability score
}
class Ability < ActiveRecord::Base {
has_many :attributions
has_many :characters, through: :attributions
}
As you can see, the characters each have their own set of abilities. I figure I may do the same thing for character races, but I am unsure of any best practices in this area. I could probably use the same join model, creating something like this (Character
would stay the same, except for an overridden abilities
method, perhaps):
class CharacterRace < ActiveRecord::Base {
# I am not sure if this will actually work, but I hope
# you understand what I am trying to do with this
has_many :racial_ability_traits, class_name: "Attribution"
has_many :abilities, through: :racial_ability_traits
}
class Attribution < ActiveRecord::Base {
# The following two are just like before
belongs_to :character
belongs_to :ability
# This field would be new
belongs_to :character_class
}
class Ability < ActiveRecord::Base {
# These are the same as above
has_many :attributions
has_many :characters, through: :attributions
# These would be new, and I am not sure if it will work, but
# I hope you understand what I amm trying to do with this
has_many :racial_traits, class_name: "Attribution"
has_many :character_races, through: :racial_ability_traits
}
However, even if this would work, I somehow feel like having the same join model for different joins (even if the purpose is pretty much the same) is kind of an ugly approach. I could, of course, create a different kind of join like this:
class CharacterRace < ActiveRecord::Base {
has_many :abilities, through: :attributions
# These are for the racial bonus
has_many :racial_ability_bonuses
has_many :abilities, through: :racial_ability_bonuses
}
class RacialAbilityBonus < ActiveRecord::Base {
belongs_to :character_race
belongs_to :ability
}
class Ability < ActiveRecord::Base {
has_many :attributions
has_many :abilities, through: :attributions
# These are for the racial bonus
has_many :racial_ability_bonuses
has_many :character_races, through: :racial_ability_bonuses
}
This would probably work, but there is a problem. Not only the races can have such traits/bonuses. A magical item might also give an ability bonus. Therefore, an Item
model should also be given a has_many :abilities, through: :item_ability_bonus
association. Continuing down the road explained above, this would cause a lot of join models.
I am therefore asking you guys if you know any good approach for handling this problem. Any suggestion about making my existing code better is welcome as well.
I am very thankful for all of your serious answers. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只需要多态关联。
I think you just need polymorphic associations.