elixir - Access foreign relationship field through template -
let's have model in phoenix app.
defmodule rumbl.video use rumbl.web, :model schema "videos" field :url, :string field :title, :string field :description, :string belongs_to :user, rumbl.user belongs_to :category, rumbl.category timestamps() end @required_fields ~w(url title description) @optional_fields ~w(category_id) def changeset(model, params \\ :empty) model |> cast(params, @required_fields, @optional_fields) end end
and category field represents relationship category model has field called name.
then in template have this:
<%= video <- @videos %> <tr> <td><%= video.user_id %></td> <td><%= video.url %></td> <td><%= video.title %></td> <td><%= video.description %></td> <td><%= video.category_id %></td> <td class="text-right"> <%= link "show", to: video_path(@conn, :show, video), class: "btn btn-default btn-xs" %> <%= link "edit", to: video_path(@conn, :edit, video), class: "btn btn-default btn-xs" %> <%= link "delete", to: video_path(@conn, :delete, video), method: :delete, data: [confirm: "are sure?"], class: "btn btn-danger btn-xs" %> </td> </tr> <% end %>
as can see, can access video.category_id
how can access video.category.name make more human readable?
edit: video controller index , categories part
defmodule rumbl.videocontroller use rumbl.web, :controller alias rumbl.video def action(conn, _) apply(__module__, action_name(conn), [conn, conn.params, conn.assigns.current_user]) end def index(conn, _params, user) videos = repo.all(user_videos(user)) |> repo.preload(:category) render(conn, "index.html", videos: videos) end alias rumbl.category plug :load_categories when action in [:new, :create, :edit, :update] defp load_categories(conn, _) query = category |> category.alphabetical |> category.names_and_ids categories = repo.all query assign(conn, :categories, categories) end end
i'm assuming you're loading videos in controller:
videos = repo.all(video)
this not load associated records. that, can use repo.preload
:
videos = repo.all(video) |> repo.preload(:category)
now each video have category
field loaded fields , can <%= video.category.name %>
in template.
Comments
Post a Comment