krut

krut

How is action set for embedded changeset?

I noticed this behavior while trying to pattern match the :changes on a changeset with an embedded schema.

I have a simple changeset/2 function defined on an embedded schema for a Webhook:

defmodule Webhook do
  embedded_schema do
    field :webhook_id, :string
    field :topic, :string
  end

  def changeset(schema, changes) do
    Ecto.Changeset.cast(schema, changes, [:webhook_id, :topic])
  end
end

Which exists on a parent Account schema:

defmodule Account do
  schema "accounts" do
    field :name, :string
    embeds_many :webhooks, Webhook
  end

  def for_insert(changes) do
    %__MODULE__{}
    |> Ecto.Changeset.cast(changes, [:name])
    |> Ecto.Changeset.cast_embed(:webhooks)
  end
end

When Webhook.changeset/2 is triggered from the cast_embed/3 in Account, action: :insert is automatically added to the embedded changeset in the returned :changes. When I call Webhook.changeset/2 directly, however, it returns action: :nil.

I assume the :action is somehow being determined by cast_embed/3 then, but I’m struggling to find exactly where that happens and / or what the reasoning is?

Marked As Solved

LostKobrakai

LostKobrakai

cast_embed does indeed add those actions, as it knows which embeds where available beforehand and it can therefore decide which elements are to be inserted, updated or deleted.

When running just the changeset this information is not available, so it can only be added by Repo calls, which know which action actually happened.

Also Liked

LostKobrakai

LostKobrakai

Nope. cast_assoc and cast_embed work by the state of the data in the source struct (preload in case of assocs) not by what is actually in the db.

Last Post!

krut

krut

Got it, thanks for elaborating :slightly_smiling_face:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement