wayoh22
wayoh22

Reputation: 176

Pass in MODEL_id to another model params

I have two models: Game and Assignment. When I create a Game, I want to automaticall create an Assignment to go along with that game, hence the association between the two. In my Game controller I have:

def create
    @game = Game.new(game_params)
    @assignment = Assignment.new(assignment_params)

    respond_to do |format|
      if @game.save
        format.html { redirect_to @game, notice: "Game was successfully created." }
        format.json { render :show, status: :created, location: @game }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @game.errors, status: :unprocessable_entity }
      end
    end
  end

private
   def game_params
      params.require(:game).permit(:home_team, :away_team)
    end

    def assignment_params
      params.require(:assignment).permit(@game.game_id)
    end
end

How do I pass in the game_id to the Assignment params when the Game is created? My models below incase they're needed. There is a game_id column in my Assignment model.

class Game < ApplicationRecord
    has_one :assignment, dependent: :destroy
    has_many :users, through: :assignments
end

class Assignment < ApplicationRecord
    belongs_to :game

    belongs_to :center_referee, class_name: 'User', foreign_key: "user_id"
    belongs_to :assistant_referee_1, class_name: 'User', foreign_key: "user_id"
    belongs_to :assistant_referee_2, class_name: 'User', foreign_key: "user_id"
end

Game Form


<%= simple_form_for(@game) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :home_team %>
    <%= f.input :away_team %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

Game Controller

def new
    @game = Game.new
  end

  # POST /games or /games.json
  def create
    @game = Game.new(game_params)

    respond_to do |format|
      if @game.save
        format.html { redirect_to @game, notice: "Game was successfully created." }
        format.json { render :show, status: :created, location: @game }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @game.errors, status: :unprocessable_entity }
      end
    end
  end

Upvotes: 0

Views: 145

Answers (1)

Claeusdev
Claeusdev

Reputation: 503

Right of the top of my head, you could simply run a simple callback inside the Game model as follows:

after_create :create_assignment    

def create_assignment
  Assignment.create(game_id: id, center_referee_id: , assistant_referee_1_id:, assistant_referee_2_id:)
end

This way you handle it once at the model level. Every game created automatically creates an assignment.

Also if the referees are not required, you may pass an optional: true flag to the belongs_to in the assignment model. that way you can safely create the games. because currently, it is not clear how you're getting the referee details from.

Upvotes: 1

Related Questions