I am trying to get nested datas with Ecto.query, but my code doesn’t work :
def myfunction(int_c_id, int_d) do
a_query = from a in A, where: a.d == ^int_d
b_query = from b in B, preload: [a: ^a_query]
Repo.one from c in C, where: c.id == ^int_c_id, preload: [b: ^b_query]
end
The error message is “cannot encode association :a from Project.B to JSON because the association was not loaded”.
It’s not possible to proceed like that to get nested datas ?
At least for me, Ecto questions are pretty hard to approach if there is just a small pseudo-example offered without a glaringly obvious problem on display … it may help if you could provide a minimal example that could be run locally …
@derive {Poison.Encoder, only: [:id, :var_a, :var_b, :var_c, :a]}
schema "bs" do
field :var_a, :integer
field :var_b, :integer
field :var_c, :integer
has_many :a, Project.A
end
I don’t understand why Poison.Encoder can’t encode the association…
I don’t think so (though I’m not sure, I don’t use preloads 99% of the time), you have to put all preloads at the top level (you can preload nested from the top level though).