helicon

helicon

Update nested struct with Ecto

Hello!
I have 2 schemas: Asset and File.

# asset schema
has_many :files, File
# file schema
belongs_to :asset, Asset

So it looks like:

%Asset{
   __meta__: #Ecto.Schema.Metadata<:loaded, "assets">,
   id: 1,
   name: "name",
   files: []
 }

Each time i need to update my asset.files (for example: add one file, then add yet 3 files in 10 minutes).
Actually I do not really understand how works put_assoc function, but it not saves old data, it removes data and set new data. Or maybe build_assoc is the only way to handle that?!
Maybe i’m explaining too bad, but it must work like in Rails that command asset.files << new_file

Most Liked

jeremyjh

jeremyjh

Two options. Either just do inserts directly to Files that include asset_id, or if you will always have the entire collection of files loaded, you can use nested changesets. So in your Asset changeset, you will have a function: cast_assoc(:files, required: false, with: &File.changeset/2)

When you update Asset though, you need to include at least the id for all existing files, as well as the fields you want to set for any new files. The former option is the only safe one if concurrent updates are possible; by default you will get an exception if you leave an ID out of your new payload.

jeremyjh

jeremyjh

Yes this was what I was referring to. That means there are existing associations you have not included in your payload. After the first execution there are two files that were assigned IDs. You need to include those in your next params:

params = %{
  files: [
    %{id: file1.id},
    %{id: file2.id},
    %{filename: "name3"},
    %{filename: "name4"} 
  ]
}

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement