defmodule Aelita2.Project do
  use Aelita2.Web, :model

  alias Aelita2.LinkUserProject
  alias Aelita2.User

  schema "projects" do
    field :repo_xref, :integer
    field :name, :string
    belongs_to :installation, Aelita2.Installation
    many_to_many :users, User, join_through: LinkUserProject

    timestamps()
  end

  def by_owner(owner) do
    from p in "projects",
      select: %{name: p.name, id: p.id},
      where: p.owner == ^owner
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:repo_xref, :name])
    |> validate_required([:repo_xref, :name])
  end
end

defmodule Aelita2.Project do
  use Aelita2.Web, :model

  alias Aelita2.LinkUserProject
  alias Aelita2.User

  schema "projects" do
    field :repo_xref, :integer
    field :name, :string
    belongs_to :installation, Aelita2.Installation
    many_to_many :users, User, join_through: LinkUserProject

    timestamps()
  end

  def by_owner(owner_id) do
    from p in LinkUserProject,
      where: p.user_id == ^owner_id,
      preload: :project
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:repo_xref, :name])
    |> validate_required([:repo_xref, :name])
  end
end

Update model to use the `many_to_many` relationship
Update model to use the `many_to_many` relationship

bors-ng/bors-ng,bors-ng/bors-ng,bors-ng/bors-ng