Hey guys, I’m currently struggling with Ecto and its Repo.update() method.
I have a struct Post and a struct Author.
The relation is Post belongs_to Authors and contains the foreign key user_id.
The Author has been preloaded.
My goal is now to change the user_id to a different user and return the updated Post. I am using the default scaffolding method for the update:
def update_post(%Post{} = post, attrs) do
post
|> Post.changeset(attrs)
|> Repo.update()
end
I call it with the attrs %{user_id: 123}. If I call the update method, I correctly get back a tuple {:ok, post}, but the preloaded Author in this post, has not been updated. So the result is like this:
%Post{
title: "...",
content: "...",
user_id: 123, # <-- this is the updated user_id
author: %{
id: *567*, # <-- this is still the old user_id
name: "old_user",
....
}
Look at the Ids, they are not what I want.
How can I make sure, that the updated user is also returned? I’m not sure if I have to preload again or something else. The only dirty workaround I came up with, is fetching the Post and preload the Author again.
How would I use it? I’d like to fetch the author again, but that fied is defined using belongs_to, which does not accept the :read_after_writes option.