klo
I have a schema that uses embeds_many and would like it to also allow for the list to be empty if there is no Actors. e.g.
id: "The Matrix",
actors: []
my schema:
field(:id, :string, primary_key: true)
embeds_many(:actors, Actor, on_replace: :delete)
field(:updated_timestamp, :map)
field(:updated_at, :utc_datetime)
end
my changeset:
def changeset(movie, %{} = params, opts \\ []) do
now = Keyword.get(opts, :now, DateTime.utc_now())
movie
|> cast(params, [
:id
])
|> cast_embed(:actors, with: &Actor.changeset/2, required: true)
|> Schema.put_updated_bson_timestamp(now)
|> Schema.put_updated_at(now)
|> validate_required(@required_fields)
end
When supplying required: true to cast_embed and the movie does not have any actors, we get a {:error, "actors can not be blank"}. When taking off the required: true the actors field can be missing completely and it is still valid. Is this an edge case where I would have to write my own validation for the :actors field?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joey_the_snake
What kind of issue are you running into by keeping it optional?
embeds_manyfields have a default of empty list so if they’re not provided they will be set to[]. Is there some kind of edge case you’re running into that isn’t working as expected?klo
i would like to enforce that the field should be there even if it is empty. If
required: trueis set and the actors list is[]it will say the changeset is invalid because the actors list is empty..joey_the_snake
The field will have a default value of
[]because it’s anembeds_many. So if you make it optional it won’t raise and it will be set to[]automatically if not there. This should suffice? Or am I mistaken?klo
When i wrote a test for it and deleted the
:actorskey from theMovieschema the changeset is still valid but my intentions is for the changeset to not be valid when the:actorskey is missing. It should still be valid when actors is a empty list though.Keeping it optional will allow for the key to not be there at which is not what i am after. However using the
require: truei am not able to leave the list as empty as it detects it is emptyjoey_the_snake
Oh ok I think I see now. You want to protect against someone manually overwriting it to
nil? You won’t be able to do it withcast_embedso you’ll have to make your own function. You can useget_fieldto see if it isnilin the changes or the original data.klo
ahh ok ok ill give it a shot and report back.
klo
Ended up writing my own function to check if the field is included inside of the params and validating it that way.