Repo.insert returning preloaded associations?
Asked Answered
C

0

6
Repo.insert(changeset)

returns {:ok, struct}, but struct has no preloaded associations. Am I forced to select query to preload it's associations? Or can I avoid a useless query somehow?

Concent answered 19/1, 2018 at 22:4 Comment(8)
You are going to have to call Repo.preload/3 yourself.Sanjak
|> Repo.insert |> Repo.preload(:posts) like this?Concent
Yup, like that. BUT, you just inserted the recorded, it is most likely there is nothing to preload. :) So you can also explicitly set the field to an empty list, if you want to.Neuman
If you use Repo.insert!, yes. If you just use Repo.insert, you are going to have to unwrap the tuple first. @JoséValim, It could belong_to another record, so it is possible there is something to preload.Sanjak
or a cast_assocBituminous
@Bituminous cast_assoc would only cast a related record but not preload it when inserting.Gar
@Gar no i was replying to José and Justin. There would be something to preload even if it wasn't belongs_to, if you were using cast_assoc on itBituminous
@Bituminous apologies mate, yeah, in this case you're right :)Gar

© 2022 - 2024 — McMap. All rights reserved.