构建关联对象时,Rails重用ID?

时间:2019-01-30 01:02:32

标签: ruby-on-rails ruby database

对于Rails和开发人员来说,这还算是新手,所以如果我对这个问题的理解错了,那么对不起。

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

这是我遇到的问题。代码如下。假设我为团队制作了5局比赛。它的id为1,可以正常工作并添加适当的局。但是,如果我随后出于任何原因删除了该游戏并制作一个新游戏,它将创建一个新游戏,该游戏的ID也为1-但这一次它有10局。再做一次,您将获得15局游戏,依此类推。服务器似乎可以很好地保存和删除游戏,但是无论我删除什么,游戏ID是否都应该不断增加?关于自动递增,我没有做任何更改。

这是我的控制者:

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

很抱歉在这里违反礼节,这是长期读者第一次登台!

1 个答案:

答案 0 :(得分:4)

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

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

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

但是,如果您不想(或不能)使用其他数据库,则可以使用以下方法解决delete问题:

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

解释上面的代码:

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