【问题标题】:Model associations clarification in Rails Active RecordRails Active Record 中的模型关联说明
【发布时间】:2014-01-30 18:25:02
【问题描述】:

我正在尝试弄清楚如何最好地进行模型关联。我有 3 张桌子。

玩家团队团队玩家

我的大纲是一个球员可以属于多个球队。一个团队可以有多个玩家,但是在我的 teamplayers 表中,我有 2 个字段 teamidplayerid(不包括主键 ID)。

例如:

Player has id of 1000
team has id of 501

在 teamplayers 表中,将存储为:

 team_id    player_id
  501        1000

那么我将如何在我的模型中设计关系 belongs_tohas_many

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您可以按以下方式实现:

    class Team < ActiveRecord::Base
      has_many :team_players
      has_many :players, :through => :team_players
    end
    
    class Player < ActiveRecord::Base
      has_many :team_players
      has_many :teams, :through => :team_players
    end
    
    class TeamPlayer < ActiveRecord::Base
      belongs_to :player
      belongs_to :team
    end
    

    相应的迁移可能如下所示:

    class CreateTeamPlayers < ActiveRecord::Migration
      def change
        create_table :teams do |t|
          t.string :name
          t.timestamps
        end
    
        create_table :players do |t|
          t.string :name
          t.timestamps
        end
    
        create_table :team_players do |t|
          t.belongs_to :player
          t.belongs_to :team
          t.timestamps
        end
      end
    end
    

    然后,如果您想获取特定球队的球员,就这样做

    @team = Team.first (do as per your requirement)
    @team.players
    

    另外,要获取玩家的球队,

    @player = Player.first (do as per your requirement)
    @player.teams
    

    H2H :)-

    【讨论】:

    • 对应的迁移是新的ruby文件吗?
    • 迁移文件中的belongs_to也设置关系吗?
    • 是的,该迁移文件可用于创建具有所需关系的表。此外,belongs_to 将设置 teamplayers 表中的关系。
    • 如果我更改了迁移文件,是否会和 rake db:migrate 重新运行?
    • 好的,我能够做到这一点,但我如何使用它作为 html rails html 的循环?像这样? &lt;select id = "Plyrs" style = float:middle size = 10&gt; &lt;% team.find(1).players do |f| %&gt; &lt;option&gt;&lt;%= f %&gt;&lt;/option&gt; &lt;% end %&gt; &lt;/select&gt;
    【解决方案2】:

    以下应该做的:

    class Player < ActiveRecord::Base
      has_many :team_players
      has_many :teams, through: :team_players
    end
    
    class TeamPlayer < ActiveRecord::Base
      belongs_to :player
      belongs_to :team
    end
    
    class Team < ActiveRecord::Base
      has_many :team_players
      has_many :players, through: :team_players
    end
    

    【讨论】:

    • 是的,你不想要骆驼式的型号名称
    • 好的,我这样做了,我的 html 中的以下行不起作用:&lt;%= select_tag "test3", options_from_collection_for_select(Teamplayer.find(:all, :joins =&gt; :player, :conditions =&gt; {:teamid =&gt; @ids}), "teamid", "playerid") %&gt; 它给出的错误是:无法自动加载常量播放器,预期 C:/Sites/rails_projects/TestFFL_App/app /models/player.rb 来定义它
    • 好的我现在收到错误:自动加载常量播放器时检测到循环依赖。我已经将关系控制器定义为上面的答案。我已经从 rails 4.0.2 降级到 rails 4.0.0
    【解决方案3】:

    你想要 has_many through 关联。

    class Player < ActiveRecord::Base
      has_many :teamplayers
      has_many :teams, through: :teamplayers
    end
    
    class TeamPlayer < ActiveRecord::Base
      belongs_to :player
      belongs_to :team
    end
    
    class Team < ActiveRecord::Base
      has_many :teamplayers
      has_many :players, through: :teamplayers
    end
    

    http://guides.rubyonrails.org/association_basics.html

    【讨论】:

    • has_many :teamplayers 应替换为 has_many :team_players
    • 这是为什么呢?因为类名是teamplayer,表叫teamplayers
    【解决方案4】:

    这有两种可能:

    首先是:

    class Team < ActiveRecord::Base
       has_and_belongs_to_many :players
    end
    
    class Player < ActiveRecord::Base
       has_and_belongs_to_many :teams
    end
    

    第二个是:

    class Player < ActiveRecord::Base
       has_many :teamplayers
       has_many :teams, through: :teamplayers
    end
    
    class TeamPlayer < ActiveRecord::Base
       belongs_to :player
       belongs_to :team
    end
    
    class Team < ActiveRecord::Base
       has_many :teamplayers
       has_many :players, through: :teamplayers
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多