1/21/2024 0 Comments Create ecto changesetI’m tempted to write a “patch_assoc” that includes the change if present but I want to be as idiomatic as possible and not go down any unnecessary rabbit holes. Is there a standard way to put_assoc when a change is present in the params that also works if the associated item isn’t included (and so doesn’t need to be modified)? I don’t see why associations shouldn’t be handled in a way that is consistent with other changeset data while still using unique assoc functions to make sure any associated operations happen. Usually, requirements are enforced by a requirement function of some sort and fields are optional unless required. This breaks the standard way changeset/2 is used. If you don’t put a user in params it will throw a messy error. To create tasks table in our database Run this command : mix ecto.migrate To undo the changes in the migration incase of misstakes Run this command : mix ecto.rollback IO.puts ' Schema, Changeset. put_assoc in changeset/2 def changeset(settings, attrs) doĪgain, this works great but we’ve now accidentally required :user in our changesets. This works but will try to modify the associated items in a way that isn’t desired in many use cases - when a user owns an item for example. To make a change you would have to use something else that would work but now we have a changeset/2 function that doesn’t actually handle all the changes for an item. The bigger challenge is that there is no way to change the associated item using changeset/2. This isn’t a dealbreaker but it is confusing and could open the door for issues. However, the associated id field isn’t included in the list of changes in the changeset. This works but feels messy and opens the possibility of bypassing something important that Ecto would do when a new association is made. Is there a standard way to put_assoc when a association change is present in the changeset/2 params that also works if the associated item isn’t included (and so doesn’t need to be modified)? There are several options I’ve run across and they all seem to have a downside or inconsistency: Just pass the associated item’s id like a regular field. I’ve worked through several options but as far as I can tell they all work counter to the way changeset/2 works for standard fields or seem dangerous. I have a question about how to handle Ecto changesets that include associations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |