Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render many to many relationship JSON in Phoenix Framework

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?

like image 308
Gidrek Avatar asked Jan 12 '16 18:01

Gidrek


1 Answers

Using render_many/4 is correct.

If you wish to define the "role.json" render function in the same module you can do:

def render("user.json", %{user: user}) do
  %{
    id: user.id,
    name: user.name,
    roles: render_many(user.roles, __MODULE__, "role.json", as: :role)
  }
end

def render("role.json", %{role: role}) do
  %{
    id: role.id
    ... 
  }
end

Notice that we pass as: :role to the render_many function. This is because the assigns (the %{role: role}) part is inferred from the view name. In this case it is the UserView so it would be %{user: user} by default.

If you define a RoleView module then you can just move the def render("role.json") function to your new RoleView and call render_many without the as option:

...
roles: render_many(user.roles, MyApp.RoleView, "role.json")
...

Another option that may be preferable for you is to derive a protocol in your model:

defmodule App.Role do
  use App.Web, :model
  @derive {Poison.Encoder, only: [:id, :name]}

  schema "roles" do
    has_many :roles_users, App.RolesUser
    has_many :users, through: [:roles_users, :user]
    field :name, :string
    timestamps
  end

Personally I feel this couples your model to your view, so I prefer to use the first option.

like image 134
Gazler Avatar answered Oct 07 '22 04:10

Gazler