What do you have in your Schema apart from changeset functions?

From the “Programming Ecto” book:

As a general rule, we recommend putting the pure functions that manipulate
queries, changesets, and multis into their associated schema modules.

Also:

Changesets, queries, and multis are pure data structures that
describe impure actions against the database, but these actions don’t take
place until we run them through the functions provided by Repo. This creates
a clear distinction between code that has side effects and code that doesn’t.

It seems that it is advised for the query builders to be in the Schema itself.

Example from the book:

defmodule MusicDB.Music.Album do
  use Ecto.Schema
  import Ecto.Query
  alias MusicDB.Music.{Album, Artist}

  schema "albums" do
    field :title, :string
    belongs_to :artist, Artist
  end

  def search(string) do
    from album in Album,
      where: ilike(album.title, ^"%#{string}%")
  end
end
3 Likes