Doubt no def create

Going through problems in the “controller”

How do I insert the result of my “select” into a column in database?

Controller:

  def new(conn, _params) do
    changeset = Disp.changeset(%Disp{})
    render(conn, "new.html", changeset: changeset, cities: Repo.all(from(c in Local, select: {c.city, c.id})))
  end

  def create(conn, %{"disp" => disp_params}) do
    changeset = Disp.changeset(%Disp{}, disp_params)
        case Repo.insert(changeset) do
      {:ok, disp} ->
        conn
        |> disp.city_id(@cities)   ######################################## << Doubt
        |> put_flash(:info, "Availability created successfully.")
        |> redirect(to: disp_path(conn, :index))
      {:error, changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

How would it be?

1 Like

If you want to add cities to disp, then I would create additional function in disp model and create function where I pass list of cities ID’s which I sync/update using Repo.insert.

1 Like

Thanks for the reply @krapans .
I did some tests, but to no avail.

1 Like

Problem has been resolved.
Making changes to the model.
Thanks.

1 Like