I was trying to update my attributes tags which a list (many_to_many) but the update deletes the old data and put the new one ! how Can I do so I keep the old ones and add other data?
thank you
If you’re using many_to_many you need to preload all of the existing records so that it can diff the ones you’re providing against the ones that are known to exist. Then it will add any new ones, remove any old ones, etc.
Example:
schema = schema
|> Repo.preload(:other_schemas)
schema
|> Schema.changeset(%{})
|> Ecto.Changeset.put_assoc(:other_schemas, [schema | schema.other_schemas]))
|> Repo.update
1 Like
Can you give me an exemple please?
Thank you