Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

elixir - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...