From bd15d91cdd17ec9ad7435fcb37c0011e12584c0d Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 22 Jan 2023 21:24:00 -0800 Subject: [PATCH] Fix grid character creation Grid characters were only replacing the character when replacing an existing character, so the mods were persisted. This creates a new GridCharacter every time a replacement happens and destroys the old one. --- .../api/v1/grid_characters_controller.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/grid_characters_controller.rb b/app/controllers/api/v1/grid_characters_controller.rb index 23d9381..74f521e 100644 --- a/app/controllers/api/v1/grid_characters_controller.rb +++ b/app/controllers/api/v1/grid_characters_controller.rb @@ -21,17 +21,16 @@ module Api conflict_view = render_conflict_view(conflict_characters, incoming_character, character_params[:position]) render json: conflict_view else - # Replace the grid character in the position if it is already filled + # Destroy the grid character in the position if it is already filled if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists? character = GridCharacter.where(party_id: party.id, position: character_params[:position]).limit(1)[0] - character.character_id = incoming_character.id - - # Otherwise, create a new grid character - else - character = GridCharacter.create!(character_params.merge(party_id: party.id, - character_id: incoming_character.id)) + character.destroy end + # Then, create a new grid character + character = GridCharacter.create!(character_params.merge(party_id: party.id, + character_id: incoming_character.id)) + if character.save! grid_character_view = render_grid_character_view(character) render json: grid_character_view, status: :created