Today I Learned

hashrocket A Hashrocket project

Sorting an Ecto has_many relation

Today I found out that there's an option preload_order in the Ecto.Schema.has_many/3 macro that allow us to configure a sorting field and direction for a relationship. This way we can:

defmodule Post do
  use Ecto.Schema
  schema "posts" do
    has_many :comments, Comment, preload_order: [asc: :title]
  end
end
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.