【问题标题】:Dynamic associations with Rails?与 Rails 的动态关联?
【发布时间】:2013-04-03 09:37:32
【问题描述】:

我在为我正在进行的当前项目定义模型设计时有点困惑。

这是一个运动队管理应用程序,因此我可以让来自不同运动的团队添加球员。由于对于每项运动我都想为运动员存储不同的信息,所以我考虑过使用以下模型。

A team:
-> has_many soccer_players
-> has_many basketball_players
-> ...

但这似乎是重复的,我只会有很多 has_many,每个运动类型都有一个。我的问题是,因为在用户创建团队时,他会选择运动类型,我只需要定义一个关联。所以如果球队是一支足球队,我只需要'has_many soccer_players'。

我怎么能这样做?或者更好的是,我将如何以更好的方式对此进行建模?

谢谢

【问题讨论】:

    标签: ruby-on-rails activerecord model associations rails-activerecord


    【解决方案1】:

    根据 Zippie 提供的内容以及您在评论中提出的问题,我提供了一个稍作修改的版本。

    我会将其分解为短语,然后分解为 ruby​​ 类和关联, 所以如果你想让它真正多态,

    • 一个团队有很多玩家
    • 一个团队有很多团队属性
    • 玩家拥有 很多属性

    它可能会简化我们应用程序的某些部分。 团队模型本身用于验证等。 您必须确定哪些是通用属性,哪些是动态属性,例如姓名、体重、身高是通用的,因为所有玩家都有它们,所以它们可以在您的 Player 模型中,而不是在您的 Attribute 模型中。

    所以我们现在可以有这样的东西:

    class Team < ActiveRecord::Base
      has_many :players
      has_many :attributes, :as => :attributable
    end
    
    class Player < ActiveRecord::Base
      belongs_to :team
      has_many :attributes, :as => :attributable
      attr_accessible :name, :weight, :height
    end
    
    class Attribute < ActiveRecord::Base
      belongs_to :attributable, :polymorphic => true
      attr_accessible :name, :value
    end
    

    关于你的其他问题

    本质上,您将拥有一张属性表、一张球员表和一张球队表。 创建一个足球队和球员(足球 = 足球是吗?)就像这样(让我们决定我们已经创建了一个球队):

    player = Player.new
    player.name = 'Lionel Messi'
    player.attributes << Attribute.create!(:name => :playing_position, :value => :second_striker)
    @team.players << player
    @team.attributes << Attribute.create!(:name => :uefa_cups, :value => 4)
    

    您的迁移将如下所示(直接取自 Rails Guides - 稍作更改):

    class CreateAttributes < ActiveRecord::Migration
      def change
        create_table :attributes do |t|
          t.string :name
          t.string :value
          t.references :attributable, :polymorphic => true
          t.timestamps
        end
      end
    end
    

    【讨论】:

    • 我真的要检查 Rails 中的多态关联
    • 伙计,谢谢你的回答。我看不到这将如何工作。假设我想创建一个新的足球队和一些足球运动员。你能帮我解决这个问题吗?另外,迁移呢,我需要和运动类型一样多的表吗?我对这个“属性”有点困惑。
    • 那么,属性不会在任何地方定义?它们将在“运行时”创建。我的意思是,假设我想列出足球运动员拥有的所有属性。那不可能采取这种方法吧?另外,如果我想访问'Lionel Messi'的playing_position,我需要查找该球员是否有一个名为这种方式的属性,对吗?非常感谢。
    • 我之所以这么说是因为我认为没有为特定运动类型的球员预定义属性这一事实令人困惑。想象一下,我想创建一个表单来插入足球运动员,我会在其中放置哪些字段?也许是一种完全不同的方法,但很难看到。
    • @Guy 好吧,这对我来说似乎很新鲜。从来没有做过多态关联。我可以试试看...谢谢
    【解决方案2】:

    可能是这样的:

    团队:

    has_many :type_of_sports, :through => type_of_sport_on_team
    has_many :players, :through => types_of_sports
    

    type_of_sport:

    has_many :teams, :through => type_of_sport_on_team
    has_many :players
    

    type_of_sport_on_team:

    belongs_to :team
    belongs_to :type_of_sport
    

    玩家:

    belongs_to :type_of_sport
    

    附加信息后:

    Class Player < ActiveRecord::Base
         attr_accessible :name, :surname, :age
    end
    
    Class BasketballPlayer < Player
         attr_accessible :free_throw_average
    end
    
    Class SoccerPlayer < Player
         attr_accessible :penalty_scores
    end
    

    【讨论】:

    • Zippie 我将在哪里存储特定运动中球员的特定信息?我的意思是,想象一下,对于足球我想存储 attribute_1 和 attribute_2,对于篮球我想存储 attribute_3 和 attribute_4
    • 使用类继承?拥有一个 player 和一个带有自己额外属性的篮球运动员和足球运动员的子类
    • Zippie,你会如何建模这是我的问题。这实际上是我在这个问题上的主要关注点......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多