Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails–;具有多个多对多关联的模型_Ruby On Rails_Activerecord_Model_Many To Many_Has Many Through - Fatal编程技术网

Ruby on rails RubyonRails–;具有多个多对多关联的模型

Ruby on rails RubyonRails–;具有多个多对多关联的模型,ruby-on-rails,activerecord,model,many-to-many,has-many-through,Ruby On Rails,Activerecord,Model,Many To Many,Has Many Through,我目前正在RubyonRails 3.2.1中开发一个龙与地下城第四版角色表应用程序。我目前正处于一个阶段,我意识到我需要将角色种族(矮人、精灵等)与能力加成联系起来。(例如:矮人获得+2的体质加成和智慧加成作为种族特征。) 我目前有以下设置: class Character < ActiveRecord::Base { has_many :attributions has_many :abilities, through: :attributions } class Attri

我目前正在RubyonRails 3.2.1中开发一个龙与地下城第四版角色表应用程序。我目前正处于一个阶段,我意识到我需要将角色种族(矮人、精灵等)与能力加成联系起来。(例如:矮人获得+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
}
然而,即使这样做可行,我还是觉得对于不同的连接使用相同的连接模型(即使目的几乎相同)是一种丑陋的方法。当然,我可以创建一种不同类型的连接,如下所示:

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
}
class CharacterRace
这可能会起作用,但有一个问题。不仅种族可以有这样的特征/奖金。一件魔法物品也可以获得能力加成。因此,一个
项目
模型也应该通过::项目(能力)奖金
关联获得一个
拥有_许多:能力。继续沿着上面解释的道路走下去,这将导致许多连接模型

因此,我想问你们是否知道处理这个问题的好方法。任何关于改进现有代码的建议都是受欢迎的


我非常感谢你所有认真的回答。:-)

我想你只是需要。

我想你只是需要。

我记得以前读过这篇文章。谢谢您的回答,我会尽快查看。:-)我记得以前读过这篇文章。谢谢您的回答,我会尽快查看。:-)
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
}