1

私は現在、Ruby on Rails 3.2.1でDungeons &ドラゴンズ4th Edition文字シートアプリケーションを開発中です。私は現在キャラクターレース(ドワーフ、エルフ、...)に能力ボーナスを関連付ける必要があることを認識している段階にいます。 (例:ドワーフ種族として2回の憲法にボーナスや知恵能力を取得します)Ruby on Rails - 複数の多対多関連を持つモデル

は、私は現在、以下を設定している:

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はおそらく、上書きabilities方法を除いて、同じ滞在する)このようなものを作成、結合モデルを同じを使用することができます。

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モデルにはhas_many :abilities, through: :item_ability_bonusアソシエーションも指定する必要があります。上に説明した道を続けると、これはたくさんの結合モデルを引き起こします。

したがって、私はあなたにこの問題を処理するための良いアプローチが分かっているかどうか尋ねています。私の既存のコードをより良くすることについてのいかなる提案も歓迎されます。

本当にありがとうございました。 :-)

答えて

1

あなたはただpolymorphic associationsが必要だと思います。

+0

これまでにこの記事を読んだことがあります。あなたの答えをありがとう、私はできるだけ早くそれをチェックアウトします。 :-) – sindrenm

関連する問題