ryanwinchester

ryanwinchester

Help me understand the associations `on_replace: :delete` example in the docs

In Ecto.Changeset — Ecto v3.14.0 there is a part which says:

The :delete option in particular must be used carefully as it would allow users to delete any associated data. If you need deletion, it is often preferred to add a separate boolean virtual field to the changeset function that will allow you to manually mark it for deletion, as in the example below: […]

defmodule Comment do
  use Ecto.Schema
  import Ecto.Changeset

  schema "comments" do
    field :body, :string
    field :delete, :boolean, virtual: true
  end

  def changeset(comment, params) do
    cast(comment, params, [:body, :delete])
    |> maybe_mark_for_deletion
  end

  defp maybe_mark_for_deletion(changeset) do
    if get_change(changeset, :delete) do
      %{changeset | action: :delete}
    else
      changeset
    end
  end
end

However, this example is incomplete and I am having trouble applying that.

Carrying on with the comment theme, If I have this as the schema that inserts it with cast_assoc, where can I mark it as deleted? :

(I know it’s weird to have one comment but that’s the type of relationship I’m using in my actual code, so I want to keep the example similar)

defmodule Post do
  use Ecto.Schema
  import Ecto.Changeset

  schema "posts" do
    field(:title, :string)
    field(:body, :string)
    has_one(:comment, Comment)
  end

  def update_changeset(post, attrs) do
    post
    |> Repo.preload(:comment)
    |> cast(attrs, [:title, :body])
    |> cast_assoc(:comment, required: true, on_replace: :nilify)
  end
end

First 3 of 3 Posts Switch mode

nico_amsterdam

nico_amsterdam

The complete explanation is here

so in schema “comments” you have correctly:
field :delete, :boolean, virtual: true
and if you update a Post containing a Comment and this comment has the value ‘true’ for the ‘delete’ field, it will be marked for deletion, and it will be deleted.

I did find the name ‘on_replace’ confusing. It specifies what to do when you don’t send an update for a child that does exists in the database. I would call it something like: ‘on_missing’ or ‘on_no_update_for’.
And the only values it can have are :raise (default), :mark_as_invalid, :nilify, :update, or :delete.
Maybe it’s me, but what I want is an option like :ignore / :nothing.

ryanwinchester

ryanwinchester OP

But won’t that mark the latest comment for deletion?

I want the one being replaced to be marked for deletion…

nico_amsterdam

nico_amsterdam

In your case a post can have only one comment, so there is only one comment that could be deleted.
So either you update the Post with the Comment with delete=true in the comment, and that comment (with the same id) will be delete,
or you specify on_replace: :delete and update the Post without any comments or with a new comment, and the on_replace will delete the existing comment.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement