Sanjibukai

Sanjibukai

Ecto partial changeset with only changes for an embed

Hello everyone,
TL;DR: How to get a changeset with only embeds data?

I have some trouble understanding how cast_embed should work..

Let’s say I have the following “parent” schema:

schema "account" do
  field :email, :string
  ....

  embeds_one :user, User, on_replace: :delete
end

And the following (simplified) embedded schema and changeset for a user (also a JSONB in postgres):

@primary_key false
  embedded_schema do
    field :firstname, :string
    field :lastname, :string
  end

def changeset(%User{} = user, attrs) do
  user
  |> cast(attrs, [:firstname, :lastname])
  |> validate_required([:firstname, :lastname])
end

Now I’m using multi-steps form, so at one moment I only want to have a changeset for that embed only, so I have the following dedicated changeset for an Account:

def user_changeset(account, attrs) do
  account
  |> cast(attrs, [])
  |> cast_embed(:user)
  |> validate_required([:user])
end

I’m expecting with the following input to have an invalid changeset:

iex> some_account
%Account{
  email: "email@example.com,"
  user: %User{
    firstname: nil,
    lastname: nil
  }
}

iex> chgst = Account.user_changeset(some_account,%{})
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #Account<>, valid?: true>

However, since the user has no firstname and lastname, I’m expecting that the changeset is invalid.
What am I missing?

If I include the :user in the cast:

def user_changeset(account, attrs) do
  account
  |> cast(attrs, [:user])   # <--- Here
  |> cast_embed(:user)
  |> validate_required([:user])
end

I got an error that casting embeds with cast/4 for :user field is not supported, use cast_embed/3 instead.
But this is what I’m doing…

I’m even wanting to do a similar embed-only changeset for another embeds_many :members which will be an array this time..

Thank you very much..

Where Next?

Popular in Questions Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement