I’m not sure why it doesn’t work without seeing more code. (User, etc)
That said, while it’s good practice to return only the fields you need, the part of “for the sake of JSON” makes me think if you shouldn’t use Ecto’s @derive instead.
@derive looks like exactly what I’m looking for, thanks @hlx!
For the sake of future readers, my User modules looks like:
defmodule Drum.Coherence.User do
@moduledoc false
use Ecto.Schema
use Coherence.Schema
schema "users" do
field :name, :string
field :email, :string
coherence_schema()
many_to_many :projects, Drum.Projects.Project, join_through: "users_projects"
has_many :comments, Drum.Projects.Comment
timestamps()
end
end
My Comment modules looks like:
defmodule Drum.Projects.Comment do
use Ecto.Schema
import Ecto.Changeset
alias Drum.Projects.Comment
alias Drum.Projects.Task
alias Drum.Coherence.User
schema "comments" do
field :content, :string
belongs_to :user, User
belongs_to :task, Task
timestamps()
end
end
(changesets not shown in either for the sake of clarity)
This is something I’ve run into as well. I ended up created private functions to clean up the redundant associations and meta data, rather than trying to use the select option in the query.
I’m not sure if it’s the best solution, but it’s working for the moment.