Ecto Phoenix - get last record in table
Today I've Learned postGiven: I want last Document And: application name is MyApp
require Ecto.Query
last_record = Ecto.Query.from(d in MyApp.Document, limit: 1, order_by: [desc: d.inserted_at]) |> MyApp.Repo.one
Alias version
import Ecto.Query
alias MyApp.Repo
alias MyApp.Document
last_record = from(d in Document, limit: 1, order_by: [desc: d.inserted_at]) |> Repo.one
title = "foobar"
last_with_title = from(d in Document, where: d.title == ^title, limit: 1, order_by: [desc: d.inserted_at]) |> Repo.one
Entire blog website and all the articles can be forked from this Github Repo