defmodule Ami.City do use Ecto.Schema import Ecto.Query alias Ami.{ Repo, Country, Profile, User } schema "cities" do field(:name, :string) field(:iso, :string) belongs_to(:country, Country) has_many(:profiles, Profile) end def search_by_name(query) do from(c in __MODULE__, where: ilike(c.name, ^"%#{query}%")) |> Repo.all() end def for_select_field() do from( c in __MODULE__, order_by: [asc: c.name], select: [:id, :name] ) |> Repo.all() end end