Cannot perform Repo.get/2 because the given value is nil

A score is calculated on create. It is not supposed to be recalculated in any other action, including :edit.

The following works on :create action, but not on the :edit action (because calculate_score/1 invokes Game.ScoreSystem.get_team!/1 which invokes Repo.get!(Team, id).

  1. What’s the correct way to use a different Changeset function on an :edit action? e.g., edit_action_changeset/2 Do I simply invoke this function in the controller’s :edit action? How would I define that it’s an :edit action?
  2. Or, how could I detect the Changeset action and ignore the put_change/3 function if it’s not a :create action?
def changeset(team, attrs) do
 team
 |> put_change(:score, calculate_score(attrs["game_id"] || team.game_id))
end

Definitely do not try and detect the action. Just make another changeset function, it’s perfectly okay to have as many as you need.

2 Likes

thanks! that’s exactly what I did (used a separate Changeset function per controller action, as needed)