Warning: undefined module attribute

I currently have an application that does a select in the bank and shows me everything it has in a single column …
I needed to add a “where” and pull this function …
Ex…

My controller

  def new(conn, _params) do
    changeset = Account.change_pedido(%Pedido{})
     render(conn, "new.html", changeset: changeset,
      cod_cliente: Repo.all(from(c in Cliente, select: {c.fantasia_cliente, c.cod_cliente})),
      tipo_cliente: Repo.all(from(c in Cliente, select: {c.tipo_cliente, c.tipo_cliente}, where: c.cod_cliente == @cod_cliente)))
  end

As you can see, cod_cliente needed to pull @tipo_cliente

It just gives me a warning,
warning: undefined module attribute @cod_cliente, please remove access to @cod_cliente or explicitly set it before access

Would have any suggestions for solving my problem?
Because the select is coming out blank

Since the query for typo_client depends on cod_cliente, you would need to save it into a variable.

  def new(conn, _params) do
    changeset = Account.change_pedido(%Pedido{})
    cod_cliente = Repo.all(from(c in Cliente, select: {c.fantasia_cliente, c.cod_cliente}))
    typo_client = Repo.all(from(c in Cliente, select: {c.tipo_cliente, c.tipo_cliente},
                                              where: c.cod_cliente == ^cod_cliente))
    
    render(conn, "new.html", changeset: changeset,
      cod_cliente: cod_cliente,
      tipo_cliente: typo_client)
  end
2 Likes

Everything worked,
Thank you

This is what confused me as well at the first days in Phoenix. There is a difference on what the @identifier construct means in the template and in the module. In the Phoenix template, usage of @assign1 is just a shorthand for Map.get(assigns, :assign1). In Elixir, @something is a compile-time module attribute.

Thus, you can’t use @cod_cliente in the controller.

I think it should be more emphasized. I remember my thoughts, “how the heck this module attribute appeared in the template and where it is set?”

2 Likes