Problem with retrieving relations in views

Can someone tell me how I can get
Identify an ID, get the name of another table.

I currently have my “form” this way.

Daily availability media

<table class="table">
  <thead>
    <tr>
      <th>Local</th>
      <th>Disponib</th>
      <th>Date</th>

      <th></th>
    </tr>
  </thead>
  <tbody>
<%= for disp <- @disps do %>
    <tr>
      <td><%= disp.city_id %></td>
      <td><%= disp.disponib %></td>
      <td><%= disp.date %></td>

      <td class="text-right">
<!--        <%= link "Show", to: disp_path(@conn, :show, disp), class: "btn btn-default btn-xs" %>
        <%= link "Edit", to: disp_path(@conn, :edit, disp), class: "btn btn-default btn-xs" %>
        <%= link "Delete", to: disp_path(@conn, :delete, disp), method: :delete, data: [confirm: "Delete?"], class: "btn btn-danger btn-xs" %> -->
      </td>
    </tr>
<% end %>

  </tbody>
</table>

<%= link "New availability", to: disp_path(@conn, :new) %>

model 1 > disp.ex

defmodule Relat.Disp do
  use Relat.Web, :model

  schema "disps" do
    field :disponib, :float
    field :date, Ecto.Date
    belongs_to :city, Relat.City

    timestamps()
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:disponib, :date, :city_id])
    |> validate_required([:disponib, :date, :city_id])
  end
end

Model 2 > local.ex

defmodule Relat.Local do
  use Relat.Web, :model

  schema "locais" do
    field :city, :string

    timestamps()
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:city])
    |> validate_required([:city])
  end
end

Currently my index.html.eex

1 Like

Hey, @gilbertosj: I don’t understand this part. HTML table? Database table? Can you explain it?

1 Like

I currently have a table called disps,
The second table is called local.

Within “locais” there are 2 fields
ID, City

Is within “disps” there are 4 fields
ID, Disponib, date, city_id

My index.html.eex
It inserts the ID that is located in the local table, inside the city_id column.

That’s why my page is showing only the ID.
<% = for disp <- @disps of%>
<% = disp.city_id%>
<% end%>

I needed it, to recognize the ID that was entered inside “city_id” with the “locais” table.

1 Like

@gilbertosj: Your relation is bad.
Change this:

belongs_to :city, Relat.City

to: this:

belongs_to :city, Relat.Local

I also don’t saw your migration, but if it’s ok, then you can call: &Repo.preload/2 inside controller and finally: disp.city.city (double city, because you named relation and field same).

1 Like

That would be it.
In fact this & Repo.preload / 2 will be inserted in “def index”.
Could you pass an example?

Current “Controller”

def index(conn, _params) do
    disps = Repo.all(Disp)
    render(conn, "index.html", disps: disps)
  end

It would be like this?

  def index(conn, _params) do
    disps = Repo.all(Disp)
    locais = Repo.preload(disps, [:city])
    render(conn, "index.html", disps: disps, locais: locais)
  end
1 Like

@gilbertosj: no

disps =
  Disp
  |> Repo.all
  |> Repo.preload(:city) # name it same as in belongs_to

and in view:

<%= for disp <- @disps do %>
  <%= disp.city.id %>
  <%= disp.city.city %>
<% end %>
3 Likes

Thanks a lot for the help.
Everything worked.

1 Like