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)
.
- 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? - 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