Elixir Ecto: How to set a belongs_to association in a changeset
<p>I am a bit stuck in how to actually set an association with a changeset. I have this code in my model:</p>

<pre><code>defmodule MyApp.MemberApplication do
  use MyApp.Web, :model
  use Ecto.Schema
  use Arc.Ecto.Schema

  alias MyApp.Repo
  alias MyApp.MemberApplication

  schema "applications" do
    field :name, :string
    field :email, :string
    field :time_accepted, Ecto.DateTime
    field :time_declined, Ecto.DateTime
    belongs_to :accepted_by, MyApp.Admin
    belongs_to :declined_by, MyApp.Admin

    timestamps()
  end

  def set_accepted_changeset(struct, params \\ %{}) do
    struct
    |&gt; cast(params, [:time_accepted, :accepted_by_id])
    |&gt; cast_assoc(params, :accepted_by)
    |&gt; set_time_accepted
  end

  defp set_time_accepted(changeset) do
    datetime = :calendar.universal_time() |&gt; Ecto.DateTime.from_erl
    put_change(changeset, :time_accepted, datetime)
  end
end
</code></pre>

<p>I want to save an association to the <code>Admin</code> that performed a certain operation (accepting or declining an member_application) and a timestamp. The generation of the timestamp works but when I try to save an association I always get the error </p>

<pre><code>** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast_assoc/3
</code></pre>

<p>This is how I want to set the association:</p>

<pre><code>iex(26)&gt; application = Repo.get(MemberApplication, 10)
iex(27)&gt; admin = Repo.get(Admin, 16)
iex(28)&gt; changeset = MemberApplication.set_accepted_changeset(application, %{accepted_by: admin})
</code></pre>