in my context I have the below to preload owner and assignee both which belong to users
def list_tasks do
Repo.all(Task)
|> Repo.preload([:owner, :assignee])
end
in my controller for index I have something like this:
def index(conn, _params) do
tasks = Issue.list_tasks()
IO.inspect(tasks)
render(conn, "index.json", tasks: tasks)
end
the IO.inspect(tasks) prints out
[
%Task3.Issue.Task{
__meta__: #Ecto.Schema.Metadata<:loaded, "tasks">,
assignee: %Task3.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
email: "[email protected]",
id: 2,
inserted_at: ~N[2018-04-02 18:22:21.699478],
updated_at: ~N[2018-04-02 18:22:21.699486],
username: "Jill"
},
assignee_id: 2,
details: nil,
id: 1,
inserted_at: ~N[2018-04-02 18:22:21.711588],
owner: %Task3.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
email: "[email protected]",
id: 1,
inserted_at: ~N[2018-04-02 18:22:21.677877],
updated_at: ~N[2018-04-02 18:22:21.677887],
username: "Jack"
},
owner_id: 1,
status: "COMPLETE",
timespent: nil,
title: "test",
updated_at: ~N[2018-04-02 18:22:21.711598]
}
]
However the json data I get on the front end is
{"data":[{"title":"test","timespent":null,"status":"COMPLETE","id":1,"details":null}]}
I have lost assignee and owner. Am I missing anything? Was there an extra step to be taken to get the preloaded data to json form?
__meta__). Because of this, we need to take out only the fields we want. In your view, you will most likely have a render function that does this. - Justin Wood