diff --git a/lib/ecto/changeset.ex b/lib/ecto/changeset.ex index cd0daeffe9..9c80eb0eb3 100644 --- a/lib/ecto/changeset.ex +++ b/lib/ecto/changeset.ex @@ -538,12 +538,12 @@ defmodule Ecto.Changeset do case fetch_change(changeset, field) do {:ok, new_value} -> case type do - {tag, relation} when tag in @relations -> + {tag, _relation} when tag in @relations -> if opts != [] do raise ArgumentError, "invalid options for #{tag} field" end - relation_changed?(relation.cardinality, new_value) + true _ -> Enum.all?(opts, fn @@ -567,14 +567,6 @@ defmodule Ecto.Changeset do end end - defp relation_changed?(:one, changeset) do - changeset.action != :update or changeset.changes != %{} - end - - defp relation_changed?(:many, changesets) do - Enum.any?(changesets, &relation_changed?(:one, &1)) - end - @doc """ Returns the default empty values used by `Ecto.Changeset`. diff --git a/test/ecto/changeset/belongs_to_test.exs b/test/ecto/changeset/belongs_to_test.exs index f62ff36b46..5ee2177d1e 100644 --- a/test/ecto/changeset/belongs_to_test.exs +++ b/test/ecto/changeset/belongs_to_test.exs @@ -569,4 +569,31 @@ defmodule Ecto.Changeset.BelongsToTest do changeset2 = %{changeset | action: :delete} assert Relation.apply_changes(embed, changeset2) == nil end + + test "changed?/2 with assocs" do + new_profile = %Profile{name: "profile"} + + changeset = + %Author{} + |> Changeset.change() + |> Changeset.put_assoc(:profile, new_profile) + + assert Changeset.changed?(changeset, :profile) + + {:ok, existing_profile} = TestRepo.insert(%Profile{name: "profile"}) + + changeset = + %Author{} + |> Changeset.change() + |> Changeset.put_assoc(:profile, existing_profile) + + assert Changeset.changed?(changeset, :profile) + + changeset = + %Author{profile: existing_profile} + |> Changeset.change() + |> Changeset.put_assoc(:profile, nil) + + assert Changeset.changed?(changeset, :profile) + end end