Episode 111: Sock it to Me, Ecto
Elixir Outlaws - A podcast by Elixir Outlaws - Thursdays
Categories:
The Elixir Outlaws now have a Patreon. If you’re enjoying the show then please consider throwing a few bucks our way to help us pay for the costs for the show. Elixir Outlaws, 02/22/2022 On today’s episode of the Elixir Outlaws, Amos and Sean are going to talk about authentication and authorization in applications as well as creating starter applications. The forms work great for air handling and everything inside of Phoenix if you are using a changeset otherwise, you got a lot of hand jamming for yourself, says Amos. Episode Highlights JavaScript has buttons to add fields or remove fields, things like that, but it is up to you if you decide that you are going to do it with live view. Changesets don’t work super well with a list. If Amos tries to put in one thing in the list, which is already in the list, it is overriding the whole list. For the invoice cases, whenever we want to add an item to the list, all those changes that are currently there will cause ripping of the form because we are affecting the changeset, says Amos. If you change one of the items in a list, you have to pull everything out, make the one change, and put everything in this change; otherwise, you are going to overwrite the whole list. So, you are going to use the get field instead of getting change, suggests Amos. Sean asks Amos, in your live view implementation, are you taking the changeset you have and trying to find a nested chainset inside it to modify it? Once I have a list, there is this extra cognitive overhead to remember that I need to grab the original fields and make sure that those end up in the changes too, because, otherwise, I am going to overwrite the whole thing, says Amos. Sean inquire from Amos, as you said, your invoice items need to be reused. Are they possible to exist on multiple invoices? Reload doesn’t put it into the changesets once you are building changesets, and that is when it becomes an issue on the front end in the database, says Amos. Amos is going to write a blog post about changeset because he could not find anything on the Internet that talked about it directly, and he is going to take another peruse through the Ecto documentation. Normally, your validations are to a single field on your changeset, and if you are writing a custom validator, use validate change, and you can pass it a function that takes the details about that change on that field, says Sean. Sean explains that all of the things we call validate those happen as soon as you call them but prepare changes like adding a function that will be called right before you call to update or insert or delete. Amos has a function for signing up that versus inviting and signing up says - I am going to create a company, which is a very common thing in web apps. You could even get a very long way into your app and only need one user per group or per team, but there is some overhead to thinking about how you appropriately hide that if you don’t need a multi-user multi-team or multi-group, says Sean. 3 Key Points If you think about the database tables, delete is like on-cast on delete cascade. There is no analog, but you need to delete the associated record if you dissociate, says Sean. When you get your response from your multi, you have to rebuild your form changeset out of that new response and send it out, which sometimes doesn’t build in the same way you want for actually displaying the first time, says Amos. If Sean was building a new web app from scratch and it had some concept of or might feet in the future need some concept of teams, he would probably go ahead and build it from the beginning. Tweetable Quotes “I spent the last two weeks on the bugs in edge cases, and I guess they weren’t really edged cases.” – Amos “When we want to remove something already existing, we have to keep changeset updated constantly with what is on the front end.” – Amos “You can use getfield whenever you are working with existing things. Get change makes sense when you only care about the things being added.” – Sean “There is some dissonance between I want to modify this one thing or I am modifying the collection and treating them as the same thing makes it complicated for you.” – Sean “When I am trying to go back to a phoenix form versus if I am using a multi with something that is not Phoenix, there is no reason to go back to a changeset, and I need to display some error the user.” - Amos Resources Mentioned: Podcast Editing Elixir Outlaws: Website Support Elixir Outlaws