Queries of specific words in the database for the index

I have an application that does query in the database and returns everything and shows in the index.html
In my database, there is a column(status) called ACTIVE / INACTIVE
I needed this in my index.html, only those that are active …

How do I block the inactive?

Controller

  def index(conn, _params) do
    todos = Registro.list_todos()
    render(conn, "index.html", todos: todos)
  end

Index.html
It would be the phoenix standard

You can maybe pass something into this function. Like

@spec list_todos(:active | :inactive) :: [Todo.t()]
def list_todos(status) do
  import Ecto.Query

  Todo
  |> where(status: ^status)
  |> Repo.all()
end

and in your controller

  def index(conn, _params) do
    active_todos = Registro.list_todos(:active)
    render(conn, "index.html", todos: active_todos)
  end
2 Likes

Returns an error

lib/todos/registro/registro.ex:324: value :active in where cannot be cast to type :string in query:


where: v.status == ^:ativa,
select: v

I was able to solve by adding “” to the controller.
Thank you very much…

like this…

  def index(conn, _params) do
   active_todos = Registro.list_todos("active")
    render(conn, "index.html", todos: active_vagas)
  end