Editing nested structures with multiple children in live_view forms

I am trying to make a student testing system. The task now is to implement multiple choice of answer options. That’s where I’m stuck.

My structure is as follows (omitted all unimportant fields):

  selected_options: [
    %SelectedOption{task_option_id: "some_id"}}

I collect these answers and after the test is completed, I save them to a database.

I’m running the entire structure through changeset. And because I don’t insert SelectedOption into the database, they have id: nil. I assume that this is the reason for incorrect and glitchy editing.

This has been discussed in more detail here

At the moment I don’t quite know how I can implement the functionality I need. Maybe someone has encountered something similar and knows how to organize flow correctly?

Maybe I should generate the necessary ids for the structures myself? That doesn’t sound right…

Hello and welcome!

Can you share a bit more code and what you mean by “glitchy editing”? You can deal with yet-to-be-saved fields by identifying them by their index/position in the form which is available in form structs. There is a newer way of dealing with this with sort_param and drop_param in in changesets, (docs) but I haven’t tried yet. There is this article which might help which is how I’m still dealing with this stuff. That is, if I’ve got your problem right.

Useful article. Thanks!
I was able to figure out what was wrong and find a solution

1 Like