gpt4 book ai didi

ruby-on-rails - Rails 在构建关联对象时重用 id?

转载 作者:数据小太阳 更新时间:2023-10-29 07:51:35 25 4
gpt4 key购买 nike

对 Rails 和一般开发来说还很陌生,如果我对这个问题的措辞有误,我深表歉意。

我正在创建一个应用程序,可以自动为踢球/小联盟球队生成阵容。用户可以创建一个团队,然后团队可以创建一个游戏。每场比赛有_many局。创建游戏时,它应该向该游戏添加指定数量的局数。

这是我遇到的问题。代码如下。假设我为一支球队打了一场 5 局比赛。它的 id 为 1 并且工作正常并添加了正确的局数。但是,如果我随后出于某种原因删除该游戏,并制作一个新游戏,它会创建一个 ID 也为 1 的新游戏——但这次它有 10 局。再做一次,你就会得到一场 15 局的比赛,依此类推。服务器似乎可以很好地保存和删除游戏,但无论我删除什么,游戏 ID 不应该一直增加吗?我没有更改任何关于自动递增的内容。

这是我的 Controller :

class GamesController < ApplicationController
before_action :set_team

# GET /games
# GET /games.json
def index
@games = Game.all
end

# GET /games/1
# GET /games/1.json
def show
@game = Game.find(params[:id])
@team = @game.team
end

# GET /games/new
def new

@game = @team.games.build
@game.no_of_innings = nil
end

# GET /games/1/edit
def edit
end

# POST /games
# POST /games.json
def create

@game = @team.games.build(game_params)
@game.user = current_user


respond_to do |format|
if @game.save

(@game.no_of_innings).times do
@game.innings.build
@game.save
end

format.html { redirect_to @game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /games/1
# PATCH/PUT /games/1.json
def update
respond_to do |format|
if @game.update(game_params)
format.html { redirect_to @game, notice: 'Game was successfully updated.' }
format.json { render :show, status: :ok, location: @game }
else
format.html { render :edit }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end

# DELETE /games/1
# DELETE /games/1.json
def destroy
@game.destroy
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
format.json { head :no_content }
end
end

def generate_lineup
@game = Game.find(params[:id])
@team = @game.team
@game.generate_lineup(@team)

render :show
end

private
# Use callbacks to share common setup or constraints between actions.
def set_team
@team = Team.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def game_params
params.require(:game).permit(:no_of_innings, :opponent, :date)
end

def inning_params
params.require(:inning).permit(:p, :c, :first, :third, :lr, :rr, :l, :lc, :rc, :r, :bench)
end

end

还有我的游戏模型:

class Game < ApplicationRecord
has_many :innings
belongs_to :user
belongs_to :team

def generate_lineup(t)
clear_all
roster = get_working_roster(t)
roster = create_bench_order(roster)
set_defense(self, roster)
end

#get working roster of people
def get_working_roster(t)
Player.all.select { |p| (p.team_id == team.id) && (p.active == true) }
end

def clear_all
self.innings.each do |inning|
inning.p = nil
inning.c = nil
inning.first = nil
inning.third = nil
inning.lr = nil
inning.rr = nil
inning.l = nil
inning.lc = nil
inning.rc = nil
inning.r = nil
inning.bench = nil
end
end

def create_bench_order(players)
kicking_order = []
guys = []
girls = []
players = players.shuffle
players.each do |player|
if player.gender == 'female'
girls << player
else
guys << player
end
end

if guys.length > girls.length
bigger = guys
smaller = girls
elsif girls.length > guys.length
bigger = girls
smaller = guys
elsif guys.length == girls.length
kicking_order = guys.zip(girls).compact.flatten
return kicking_order
end

(smaller.length).times do |x|
kicking_order << bigger[0]
bigger.shift
kicking_order << smaller[0]
smaller.shift
end

index = 0
while bigger.any?
kicking_order.insert(index, bigger[0])
bigger.shift
index += 3
end
return kicking_order
end

def set_defense(game, players)

game.innings.each do |inning|
bench = []
bench_no = players.length - 10

bench_no.times do
player = players[0]
bench << player.name
players.shift
players << player
end

bench_display = ""
bench.each do |x|
bench_display += x + ", "
end
inning.bench = bench_display

playing = players[0...-(bench_no)]
playing = playing.shuffle

playing.each do |plr|
player_prefs = [plr.p1, plr.p2, plr.p3, plr.p4, plr.p5, plr.p6, plr.p7, plr.p8, plr.p9, plr.p10]

index = 0

until index > 9 do
if free?(inning.p) && player_prefs[index] == 'p'
inning.p = plr.name
break
elsif free?(inning.c) && player_prefs[index] == 'c'
inning.c = plr.name
break
elsif free?(inning.first) && player_prefs[index] == 'first'
inning.first = plr.name
break
elsif free?(inning.third) && player_prefs[index] == 'third'
inning.third = plr.name
break
elsif free?(inning.lr) && player_prefs[index] == 'lr'
inning.lr = plr.name
break
elsif free?(inning.rr) && player_prefs[index] == 'rr'
inning.rr = plr.name
break
elsif free?(inning.l) && player_prefs[index] == 'l'
inning.l = plr.name
break
elsif free?(inning.lc) && player_prefs[index] == 'lc'
inning.lc = plr.name
break
elsif free?(inning.rc) && player_prefs[index] == 'rc'
inning.rc = plr.name
break
elsif free?(inning.r) && player_prefs[index] == 'r'
inning.r = plr.name
break
else
index += 1
end
end
end
end
end


def free?(position)
position == nil
end

end

很抱歉这里有任何违反礼节的行为,长期读者第一次发帖!

最佳答案

这是一个非常有趣的问题!但这不是 Ruby on Rails 的问题!

ActiveRecord不控制保存的id ,他只将属性传递给数据库并恢复创建的资源,这是您的数据库的一个问题。

如果我可以向您推荐一些东西,请使用开源关系数据库,如 Mysql 或 Postgresql,但要使用原始数据库配置

但是,如果您不想(或不能)使用另一个数据库,您可以解析 delete以下问题:

class Game < ApplicationRecord
has_many :innings, dependent: destroy
...
...
end

解释上面的代码:

:dependentbelongs_to 的选项之一协会。当设置为 :destroy 时,如果记录被删除,所有关联的记录(具有 dependent: :destroy )也将被销毁!

关于ruby-on-rails - Rails 在构建关联对象时重用 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com