具有多个ID的ActiveRecord关联/种子数据

时间:2017-05-16 15:09:12

标签: ruby-on-rails activerecord

我正在为体育团队制定计划,并在ScheduleTeam之间建立多对多的关系。

我的关联似乎有效,因为我可以将schedule_id分配给team,但我想将schedule_id分配给两个Teams,或者反之亦然,所以两个Teams到一个schedule_id

完成此操作后,会将schedule_id: 1分配给team_id: 1

# Team Seed Data
team_name: 'Team One',
schedule_id: 1

我以为我可以输入schedule_id作为数组,但这不起作用。所以:

team_name: 'Team One',
schedule_id: [1, 2]

但是Team has_many Schedules(所以游戏)和Schedule has_many(两个)Teams

所以我的问题是,我可以将schedule_id: 1分配给team_id: 1team_id: 2吗?

或者,我是否需要添加迁移以反过来执行此操作,并将team_id: 1team_id: 2分配给Scheduleteam_id_hometeam_id_away或类似的东西?

按要求使用模型和迁移进行更新

模型

# team.rb
class Team < ApplicationRecord
  has_many :players
  has_many :schedules
end

# team_schedule.rb
class TeamSchedule < ApplicationRecord
  belongs_to :team
  belongs_to :schedule
end

# schedule.rb
class Schedule < ApplicationRecord
  has_many :teams
  has_many :players, through: :teams
end

# player.rb
class Player < ApplicationRecord
  belongs_to :team
  has_many :schedules, through: :teams
end

迁移

# create_teams.rb
class CreateTeams < ActiveRecord::Migration[5.1]
  def change
    create_table :teams do |t|
      t.string :team_name

      t.timestamps
    end
  end
end

# create_schedules.rb
class CreateSchedules < ActiveRecord::Migration[5.1]
  def change
    create_table :schedules do |t|
      t.string :leagueGame
      t.string :home
      t.string :homeAbr
      t.string :away
      t.string :awayAbr

      t.timestamps
    end
  end
end

# team_schedules.rb
class CreateTeamSchedules < ActiveRecord::Migration[5.1]
  def change
    create_table :team_schedules do |t|
      t.belongs_to :team, index: true
      t.belongs_to :schedule, index: true

      t.timestamps
    end
    add_column :teams, :schedule_id, :integer
  end
end

# add_column_to_players.rb
class AddColumnToPlayers < ActiveRecord::Migration[5.1]
  def change
    add_column :players, :team_id, :integer
    add_column :players, :schedule_id, :integer
  end
end

1 个答案:

答案 0 :(得分:0)

您的协会不完整; TeamSchedule都应与TeamSchedule建立关联。您当前的代码只会在TeamSchedule上添加这些关联,因此您需要更新TeamSchedule,如下所示:

# team.rb
class Team < ApplicationRecord
  has_many :players
  has_many :team_schedules
  has_many :schedules, through: :team_schedules
end

# team_schedule.rb
class TeamSchedule < ApplicationRecord
  belongs_to :team
  belongs_to :schedule
end

# schedule.rb
class Schedule < ApplicationRecord
  has_many :team_schedules
  has_many :teams, through: :team_schedules
  has_many :players, through: :teams
end

使用该设置,现在您可以使用team_schedules表格在同一时间表中配对多个团队。

尽管如此,以这种方式设置游戏并不容易,因此不要一次将TeamSchedule一个团队相关联,让我们与两个团队联系。

首先,更新您的迁移:

class CreateTeamSchedules < ActiveRecord::Migration[5.1]
  def change
    create_table :team_schedules do |t|
      t.string :home_team_id
      t.string :away_team_id
      t.string :schedule_id

      t.timestamps
    end
    add_column :teams, :schedule_id, :integer
  end
end

接下来,更新team_schedule.rb

class TeamSchedule < ApplicationRecord
  belongs_to :home_team, class_name: "Team"
  belongs_to :away_team, class_name: "Team"
  belongs_to :schedule
end

有了这些更新,现在您的TeamSchedules将在一条记录中包含两个Team ID(主页和离开)。这意味着你可以这样做:

schedule = Schedule.create(schedule_attributes)
team1 = Team.create(team_name: "Team 1")
team2 = Team.create(team_name: "Team 2")

game = TeamSchedule.create(schedule_id: schedule.id, home_team_id: team1.id, away_team: team2.id)

game.home_team.team_name
#=> "Team 1"

这种类型的关联最适合您的特定情况,您可以创建不同的游戏(即team_schedules)将任何Schedule与任意两个Teams相结合}}