LoginSignup
4
3

More than 5 years have passed since last update.

Elixir+Phoenixでorder_byで昇順・降順で更新日順に並び替える

Posted at

たとえばブログ記事としてPostモデルがあったとして

  schema "posts" do
    field :title, :string
    field :body, :string

    timestamps
  end

最新順に並び替えたい

こうします。

  def index(conn, _params) do
    query = from(p in Post, order_by: [desc: p.inserted_at])
    posts = Repo.all(query)
    render(conn, "index.html", posts: posts)
  end
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3