How do I get access to DB methods where, last etc

I am in a LiveVIew and I am trying to get access to the where and last methods per this Stackoverflow query answer:

  alias App.Entry
  import Ecto.Query
  Entry |> where(title: "Page 1") |> last(:inserted_at) |>

My code is below. I get an error saying that the where and last methods are unavailable.

defmodule AppWeb.PageLive do
   use AppWeb, :live_view 
   alias App.TestBeds
   def mount(_params, _session, socket)  do

   TestBeds.list_testbeds|> where(name: "Zarnog") |> last(:inserted_at) |> IO.inspect

	 {:ok, assign(socket, testbeds: TestBeds.list_testbeds())}  


I tried importing

import Ecto.Query

I got a big error.

protocol Ecto.Queryable not implemented for [%App.TestBeds.TestBed{__meta__: #Ecto.Schema.Metadata<:loaded, "testbeds">, id: 1, name: "Zarnog", note: "best testbed!", status_action_id: 1, version: "6.01", inserted_at: ~N[2023-03-01 18:35:30], updated_at: ~N[2023-03-01 18:35:30]}] of type List. This protocol is implemented for the following type(s): Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple

My goal is to understand how to do queries of the sort expressed in my code. I see different examples all over the web.

You’re trying to pass a list to the where clause and its complaining about that.

Notice the opening [ before your struct on the error message.

(and yes, you’ll want to import Ecto.Query to access those clauses)

I have a list of data in a table (a list) and I want to retrieve the last one with a specific key that was submitted.

Get last submitted item in SOME-TABLE THAT has a key/value of X

Try creating a new function on your TestBeds module that will retrieve the record you want, something like:
App.TestBeds.TestBed |> where(name: "Zarnog") |> last(:inserted_at) |> (You could also just try this on an iex session and see how it goes)

Here you’ll be passing the schema you want to query from (App.TestBeds.TestBed) rather than a list of your existing TestBeds.

Your TestBeds.list_testbeds return a list of records. where, last and friends don’t know what to do with a list, they need a query as the first argument.

1 Like

It seems to work. Where does come from?
Is it documented anywhere?

In the StackOverflow query I assumed that was not applicable to my code.

THankx btw

If you look inside your lib/app folder, there will be a module named repo.ex.
It uses the Ecto.Repo from Ecto where you have some functions, including one which will fetch one record matching the query you passed to it.

There is also all function, which will fetch all the records matching your query (as probably the TestBeds.list_testbeds does).

There is more to it, but I don’t want to lead you on a rabbit whole, I think is best if you acquaint yourself with it on your own pace.
Reading the link I sent you about Repo will probably clarify some things.

Also, feel free to mark the answer as a solution if you want. is just a function call, there’s nothing special about that syntax. As for where you’re supposed to find out about that function, it’s covered in the Ecto docs Ecto.Repo — Ecto v3.9.4.