mekusigjinn

mekusigjinn

Idiomatic way to make ecto schema validate has_many relation AND has at least one association?

If SchemaA has_many SchemaB, what is the typical approach to validate if at least one SchemaB is associated to SchemaA?

For instance if I do

SchemaA.changeset(schema_a)

I want the resulting %Ecto.Changeset{errors: ____} to show error related to this issue if no SchemaB is associated.

Thanks in advance!

Most Liked

tfwright

tfwright

Have you checked out Changeset.add_error? It should allow you to add whatever errors you like depending on the state of the changeset or underlying data if you use it in combination with get_field and/or get_change. So for example if you want to validate that on create at least one B will be associated you can do

changeset = 
  changeset
  |> preload(:bs)
  |> cast(params, [])
  |> cast_assoc([:bs], with: &B.changeset/2)

changeset = 
  changeset
  |> get_change(:bs)
  |> case do
    [] -> add_error(changeset, :bs, "is required") # this might need to be nil not sure
    _ -> changeset
  end

Last Post!

mekusigjinn

mekusigjinn

wow I think that is very clean and would work. Thank you!!

Where Next?

Popular in Questions 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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement