hawkyre

hawkyre

Updating structs with Ecto changesets and put_assoc

Hey there, this is my first post in here! I’m “just” in need of a little help with changesets and associations.

I have a changeset I built that works wonders to create and validate structs. However, now that I want to also add functionality to update structs this one’s not gonna do it since I’m using put_assoc and passing in the params to my helper function:

def changeset(%Course{} = struct, params \\ %{}) do
    struct
    |> cast(params, [:title, :description])
    |> put_assoc(:scopes, parse_scopes(params), required: true)
    |> validate_required([:title, :description])
  end

  defp parse_scopes(params) do
    (params["scopes"] || [])
    |> Enum.map(fn name ->
      Repo.get_by(Scope, name: name)
    end)
  end

If I wanted to just update the title, I would just pass in the title through params and the existing course through struct, but that would mean the scopes would be reset to none since put_assoc destroys any association.

What is the best way to approach this problem?

Also, I’ve got another problem with this and it is that if the user passes in an invalid scope this will throw an error and I have no idea how to catch that in a clean way. I may open a new question for this later but I’d appreciate it if someone had some insight on this too.

Thanks a lot!

Edit: fixed a word

First Post!

trowl3r

trowl3r

Hey,

I don’t know, if that is, what you are looking for, but maybe try to use build_assoc since this don’t overwrite the association.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement