brucepomeroy

brucepomeroy

Unexpected behaviour Changing Ecto.Changeset with embed

Sorry I couldn’t come up with a better title for this one!

I’m trying to use an Ecto Changeset to maintain a set of changes provided by a user, eventually, when the user clicks “Save”, the changes will be written to the database but for now I’m trying to use the changes to keep track of all requested changes so I can conditionally show/hide form elements and display validation messages. Each time the user makes a change to a field, I don’t necessarily receive all the params for all the other fields again. So whenever I get a change from the user, I add that change to a changeset that is already tracking all previous changes. I’m getting some unexpected behaviour though. It occurs in the case that the user edits a field of an embedded association, changing it back to its original value.

  defmodule Page do
      use Ecto.Schema
      import Ecto.Changeset


      schema "pages" do
        embeds_one :background, Background, on_replace: :update do
          field :color, :string
         end
         timestamps()
      end

      def background_changeset(schema, params) do
        schema
        |> cast(params, [:color])
      end

      @doc false
      def changeset(schema, attrs) do
        schema
        |> cast(attrs, [])
        |> cast_embed(:background, with: &background_changeset/2)
      end
    end


    test "replacing a change in a changeset" do
      page = %Page{background: %Page.Background{color: "red"}}

      changed_to_blue = Page.changeset(page, %{"background" => %{"color" => "blue"}})
      assert changed_to_blue.changes.background.changes == %{color: "blue"}

      changed_to_green = Page.changeset(changed_to_blue, %{"background" => %{"color" => "green"}})
      assert changed_to_green.changes.background.changes == %{color: "green"}

      changed_back_to_red = Page.changeset(changed_to_green, %{"background" => %{"color" => "red"}})
      assert changed_back_to_red.changes.background.changes == %{color: "red"}

      # Assertion with == failed
      #  code:  assert changed_back_to_red.changes.background.changes == %{color: "red"}
      #  left:  %{color: "green"}
      #  right: %{color: "red"}
    end
  end

As you can see above, we can keep applying new changes to a changeset and they overwrite the previous changes, this is what I want. However, if we try to change the value back to the value contained in the Changeset’s data, the change is ignored.

Does this seem like unexpected behaviour? If so, I can post this as a Github issue for Ecto. Or am I misunderstanding/misusing changesets here?

Thanks!

Most Liked

LostKobrakai

LostKobrakai

I’m not sure this is fully unexpected behaviour. Generally you want to avoid setting a value as a change if it matches the current value. The difference here is that you already have a change. I’d be curious if the same happens without the embedded schema. If not, then it’s imho a bug because of inconsistency.

For me this kinda strengthens my opinion of changesets being a bad format for accumulating changes in. The other one is the fact that errors are never “revalidated”. I see changesets more as a short lived artefact of changes. You’re using it more like a cache for changes. Imo the cleaner solution would be using e.g. Ecto.Changeset.apply_action to apply changes between each step and use the result for the next changeset as base. Only when it comes to storing the changes fetch from the db again (or store it separately) and create a changeset for the changes between the db stored data and ones you have at runtime.

dimitarvp

dimitarvp

Yep. People trying to use them as long-lived accumulators of changes are in for trouble, as it happened here.

Changesets seem designed for one-off operations.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement