Updating a struct

I have a struct s which has_one a struct c like;
c = %C{d: 1} // c belongs_to s
s = %S{a: 1, b: c} // s has_one c

I want to update c’s d field, and updated it like;
c = Repo.preload(s, :c).c
Cs.update_c(%{d: 2}) // Cs is c’s context file, and now c shows like;
c = %C{d: 2}

And, IO.inspect shows c is properly updated.
IO.inspect c.d // 2

However, the c which belongs to s remains unchanged like;
s = Repo.preload(s, :c)
IO.inspect s.c.d // 1

Share any idea on what happened, and how to update the c which belongs_to s.

Thank Adzz and idiot for my last question.

  • Elixir’s data structures are immutable;
  • if you try to preload an association that was already previously preloaded, it will not execute a DB request again.
1 Like