I have the next models
defmodule App.User do
use App.Web, :model
alias App.User
schema "users" do
field :name, :string
has_many :roles_users, App.RolesUser
has_many :roles, through: [:roles_users, :role]
timestamps
end
end
defmodule App.Role do
use App.Web, :model
schema "roles" do
has_many :roles_users, App.RolesUser
has_many :users, through: [:roles_users, :user]
field :name, :string
timestamps
end
end
defmodule App.RolesUser do
use App.Web, :model
schema "roles_users" do
belongs_to :role, App.Role
belongs_to :user, App.User
timestamps
end
end
Is for a many to many relationship. My controller to show is
def index(conn, _params) do
users = Repo.all(User)
|> Repo.preload(:roles)
render(conn, "index.json", users: users)
end
In the view I have
def render("index.json", %{users: users}) do
%{users: render_many(users, App.UserView, "user.json")}
end
def render("show.json", %{user: user}) do
%{user: render_one(user, App.UserView, "user.json")}
end
def render("user.json", %{user: user}) do
%{id: user.id,
name: user.name,
roles: user.roles
}
When I sent the GET request I got this error
unable to encode value: {nil, "roles"}
I know that could be because user.roles
need to be formatted in some way to decode the JSON, but I have not any clue about this. I have tried in the form
def render("user.json", %{user: user}) do
%{id: user.id,
name: user.name,
roles: render_many(roles, App.UserView, "roles.json")
}
But is not working.
What is the best way to render many to many relationships in view?