[英]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
class UserTeam < ApplicationRecord
has_many :players
end
class Player < ApplicationRecord
belongs_to :user_team
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.