简体   繁体   English

Rails 6 如何将 model 的多个字段作为对另一个表的引用

[英]Rails 6 How to have multiple fields of a model as references to another table

I'm currently creating a Rails app using rails 6. I have model named user_teams and a model named players.我目前正在使用 rails 6 创建一个 Rails 应用程序。我有一个名为 user_teams 的 model 和一个名为 player 的 model。 What I want to do is have multiple fields in the user_teams model such as player_one, player_two...player_six that are each an individual reference to a player.我想要做的是在 user_teams model 中有多个字段,例如 player_one、player_two...player_six,每个字段都是对玩家的单独引用。

I have implemented this but in a slightly janky way.我已经实现了这一点,但方式有点笨拙。 I have the six fields in my user_team model and each is an integer of the id associated with the chosen player.我的 user_team model 中有六个字段,每个字段都是与所选玩家关联的 id 的 integer。 I feel like there is a much better way to do this but I can't figure out how.我觉得有更好的方法可以做到这一点,但我不知道如何做。 Any help would be greatly appreciated!任何帮助将不胜感激!

Also once I have the references set up properly, how can I validate each player reference so that duplicate players can't be in the same user_team?此外,一旦我正确设置了参考,我如何验证每个玩家参考,以便重复的玩家不能在同一个 user_team 中?

Example UserTeam player_one: 1 player_two: 1示例用户团队 player_one:1 player_two:1

Would not be allowed but不允许但

UserTeam player_one: 1 player_two: 2 UserTeam player_one: 1 player_two: 2

Would be allowed?会被允许吗?

This is a standard many to many association and what you want is a join table that joins players and teams:这是一个标准的多对多关联,您想要的是一个连接玩家和团队的连接表:

class Player < ApplicationRecord
  has_many :positions
  has_many :user_teams, through: :positions
end

class UserTeam < ApplicationRecord
  has_many :positions
  has_many :players, through: :positions
end

# generate the model and migration with:
# rails g model position player:belongs_to user_team:belongs_to
class Position < ApplicationRecord
  belongs_to :player
  belongs_to :user_team
end

Another way to set this up would be with has_and_belongs_to_many but as there is no join model there is no way to access any additional columns on the join table and you cannot join it directly.另一种设置方法是使用has_and_belongs_to_many ,但由于没有连接 model ,因此无法访问连接表上的任何其他列,您无法直接连接它。

You can validate the uniqueness of a player on a team by using validates_uniqueness with the scope option:您可以通过使用 validates_uniqueness 和scope选项来验证团队中玩家的唯一性:

class Position < ApplicationRecord
  belongs_to :player
  belongs_to :user_team
  validates :player_id, uniqueness: { scope: :user_team_id }
end 

This will prevent most duplicates but is prone to race conditions which could lead to duplicate data being inserted so it should be complimented with a unique index that enforces the constraint on the database level:这将防止大多数重复,但容易出现可能导致插入重复数据的竞争条件,因此应使用唯一索引来补充,该索引在数据库级别强制执行约束:

class CreatePositions < ActiveRecord::Migration[6.0]
  def change
    create_table :positions do |t|
      t.belongs_to :user_team, null: false, foreign_key: true
      t.belongs_to :player, null: false, foreign_key: true
      t.timestamps
    end
    add_index :positions, [:user_team_id, :player_id], unique: true
  end
end

UserTeam model should have field name & Player model should have field user_team_id UserTeam model 应该有字段name & Player model 应该有字段user_team_id

model/UserTeam.rb模型/UserTeam.rb

class UserTeam < ApplicationRecord
  has_many :players
end

model/Player.rb模型/播放器.rb

class Player < ApplicationRecord
  belongs_to :user_team
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails 6 ActiveRecord 关联,一个 model 与另一个 Z20F35E630F44DBFA8DZC3F6 的多个引用 - Ruby on Rails 6 ActiveRecord associations, one model with multiple references to another model 如何在一个表中添加对同一模型的多个引用的迁移? 红宝石/导轨 - How do I add migration with multiple references to the same model in one table? Ruby/Rails 如何在 Rails model 生成上添加对除 ID 之外的另一个字段的引用? - How to add references to another field besides ID on Rails model generation? 如何从rails上的ruby中的另一个表字段更新表字段 - How to update table fields from another table fields in ruby on rails 同一模型在另一个Rails中的两个引用:4 - Two references of the same model in another Rails: 4 Rails:来自一个表/模型的两个引用 - Rails: two references from one table/model 一张桌子上有多个Rails模型参考 - Several rails model references on one table Rails:多个引用相同的模型错误 - Rails: Multiple references to the same model error Rails模型代表多个字段 - Rails model to represent multiple fields 在Ruby on Rails中,如何创建一个表/模型,其中包含引用同一表的字段? - In Ruby on Rails, how do I create a table/model that has a field in it that references the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM