Ash Framework: variable assignment mistake (page 38)

In the “Defining an update action” section
artist = Tunez.Music.get_artist_by_id!(​"​​an-artist-id"​)
should be:
{:ok, artist} = Tunez.Music.get_artist_by_id!(​"​​an-artist-id"​)

Otherwise the next line will fail with an error about resource ID

Hi there!

In this case, no it shouldn’t - the bang version of a function (ie. get_artist_by_id! vs. get_artist_by_id) will return the object by itself, not as part of an ok or error tuple.

I’ll check and see if we need a note clarifying this…

2 Likes

I would caution against it given that this is basic Elixir. If that warrants an explanation then what about explaining pattern matching and “let it crash” ? Those are the reason why we have bang functions.

This looks like a rabbit hole to me.

Just chiming in here to say that I made a mistake while typing in the code here. I typed in artist = ...get_artist_by_id() without the bang, and it was hard to see in the console that it returned an :ok tuple. I then tried to pass the artist to the update function, and it failed with an Ash.Error.Invalid.InvalidPrimaryKey error.

I had to come find this thread to realize that I had typed it in wrong, especially since the paragraph above refers to it as the get_artist_by_id name.