I have a simple model (short version) :
defmodule MyApp.User do
use Ecto.Model
@derive {Poison.Encoder, only: [:id, :name, :email]}
schema "users" do
field :name, :string
field :email, :string
belongs_to :company, MyApp.Company
I expect the @derive to exclude :company associations while trying to encode the model, but it doesn't seem to. This happens while using Guardian serializer :
defmodule MyApp.GuardianSerializer do
@behaviour Guardian.Serializer
alias MyApp.Repo
alias MyApp.User
def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
def for_token(_), do: { :error, "Unknown resource type" }
def from_token("User:" <> id), do: { :ok, Repo.get(User, id) }
def from_token(_), do: { :error, "Unknown resource type" }
end
I'm not actually sure to understand what happens with
def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
From what i understand user = %User{} is trying to test if the object given as a parameter is a valid User changeset right ?
But i get this error instead :
cannot encode association :company from MyApp.User to JSON because the association was not loaded. Please make sure you have preloaded the association or remove it from the data to be encoded
I do not want to preload it, because it will require more dependencies to encode which doesn't work either, i would prefer to ignore it.
Why the only param in @derive doesn't work and how could I resolve this issue ?
Poison.encode!(Repo.get(User, 1))(replacing 1 with a valid user id)? - DogbertPoison.encode!/1- SylverCommentbelongs_toPost;Poison.encode!(Repo.get(Comment, 1))with@derive {Poison.Encoder, only: [:id, :content]}onComment. Can you try to reproduce the error in a new minimal app? - DogbertCompanywhich are not preloaded... but i don't want to preload or encode any of these. - SylverPoison.encode! MyApp.Repo.get(MyApp.Comment, 2) |> MyApp.Repo.preload([:post])with the same@deriveas I posted in the previous comment. - Dogbert