venomnert

venomnert

Ecto.update is not updating jsonb

The problem

I have a :map field that I am able to update on create. On update, the changeset returns a successful update;however, the database jsonb is not reflecting the changes. I’m unable to determine what the issue is.

Schema:

 schema "promo_jobs" do
    field(:content, :map, default: %{})
    field(:draft, :boolean, default: true)
    field(:end_date, :utc_datetime)
    field(:name, :string)
    field(:rolled_out, :boolean, default: false)
    field(:start_date, :utc_datetime)
    field(:take_down, :boolean, default: false)

    has_one(:landing_page_url, LandingPageUrl, on_replace: :nilify, on_delete: :nilify_all)
    belongs_to(:region, Region)
  end

  def changeset(promo_job, attrs) do
    promo_job
    |> add_content(attrs)
    |> cast(attrs, [:name, :rolled_out, :take_down, :start_date, :end_date, :content, :draft])
    |> validate_required([
      :name,
      :rolled_out,
      :take_down,
      :start_date,
      :end_date,
      :content,
      :draft
    ])
  end

  defp add_content(promo_job, attrs) do
    promo_job
    |> Map.put(:content,
      Morphix.atomorphiform!(promo_job.content)
      |> Content.new()
      |> Content.update_input_value(attrs))
  end

Controller:

  def show(conn, %{"id" => id}) do
    promo_job = Promos.get_promo_job!(id) |> IO.inspect(label: "SHOW")
    render(conn, "show.html", promo_job: promo_job)
  end

def update(conn, %{"id" => id, "promo_job" => promo_job_params}) do
    promo_job = Promos.get_promo_job!(id)

    case Promos.update_promo_job(promo_job, promo_job_params) do
      {:ok, promo_job} ->

        IO.inspect(promo_job, label: "UODATED")

        conn
        |> put_flash(:info, "Promo job updated successfully.")
        |> redirect(to: Routes.promo_job_path(conn, :show, promo_job))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "edit.html", promo_job: promo_job, changeset: changeset)
    end
  end

Context:

  def create_promo_job(attrs \\ %{}) do
    promo_job = %PromoJob{} |> PromoJob.changeset(attrs)

      promo_job
      |> Repo.insert()
  end

  def update_promo_job(%PromoJob{} = promo_job, attrs) do
    promo_job
    |> PromoJob.changeset(attrs)
    |> Repo.update()
  end

Inspect Logs:

 UODATED: %PromoRollout.Promos.PromoJob{
  __meta__: #Ecto.Schema.Metadata<:loaded, "promo_jobs">,
  content: %{
    body_promo: %{
      fields: [
        %{
          content: "abab",
          field_type: "text_input",
          name: "bg_mobile",
          placeholder: "Image url"
        },
        %{
          content: "2312312",
          field_type: "text_input",
          name: "bg_desktop",
          placeholder: "Image url"
        }
      ],
      full_html: "",
      status: false
    },
    promo_bar: %{
      fields: [
        %{
          content: "zcxzc",
          field_type: "text_input",
          name: "image_url",
          placeholder: "Image url"
        }
      ],
      full_html: "",
      status: false
    }
  },
  draft: false,
  end_date: ~U[2020-01-01 00:00:00Z],
  id: 25,
  inserted_at: ~N[2020-01-30 23:06:54],
  landing_page_url: %PromoRollout.Promos.LandingPageUrl{
    __meta__: #Ecto.Schema.Metadata<:loaded, "landing_page_urls">,
    draft: true,
    end_date: ~U[2020-01-01 00:00:00Z],
    id: 1,
    inserted_at: ~N[2020-01-25 12:20:57],
    promo_job: #Ecto.Association.NotLoaded<association :promo_job is not loaded>,
    promo_job_id: 25,
    redirected: false,
    updated_at: ~N[2020-01-30 23:30:03],
    url: "/lp/easter"
  },
  name: "testing 123",
  region: nil,
  region_id: nil,
  rolled_out: false,
  start_date: ~U[2020-01-01 00:00:00Z],
  take_down: false,
  updated_at: ~N[2020-01-30 23:30:03]
}
SHOW: %PromoRollout.Promos.PromoJob{
  __meta__: #Ecto.Schema.Metadata<:loaded, "promo_jobs">,
  content: %{
    "body_promo" => %{
      "fields" => [
        %{
          "content" => "21312321321",
          "field_type" => "text_input",
          "name" => "bg_mobile",
          "placeholder" => "Image url"
        },
        %{
          "content" => "dsfasfasfsfss",
          "field_type" => "text_input",
          "name" => "bg_desktop",
          "placeholder" => "Image url"
        }
      ],
      "full_html" => "",
      "status" => false
    },
    "promo_bar" => %{
      "fields" => [
        %{
          "content" => "xzzzxZxxzx",
          "field_type" => "text_input",
          "name" => "image_url",
          "placeholder" => "Image url"
        }
      ],
      "full_html" => "",
      "status" => false
    }
  },
  draft: false,
  end_date: ~U[2020-01-01 00:00:00Z],
  id: 25,
  inserted_at: ~N[2020-01-30 23:06:54],
  landing_page_url: %PromoRollout.Promos.LandingPageUrl{
    __meta__: #Ecto.Schema.Metadata<:loaded, "landing_page_urls">,
    draft: true,
    end_date: ~U[2020-01-01 00:00:00Z],
    id: 1,
    inserted_at: ~N[2020-01-25 12:20:57],
    promo_job: #Ecto.Association.NotLoaded<association :promo_job is not loaded>,
    promo_job_id: 25,
    redirected: false,
    updated_at: ~N[2020-01-30 23:30:03],
    url: "/lp/easter"
  },
  name: "testing 123",
  region: #Ecto.Association.NotLoaded<association :region is not loaded>,
  region_id: nil,
  rolled_out: false,
  start_date: ~U[2020-01-01 00:00:00Z],
  take_down: false,
  updated_at: ~N[2020-01-30 23:30:03]
}

You will notices that the update log is correct, it reflects all the changes I’ve made. Once we are directed to the show route, the data that’s retrieved from the db has some inconsistency. The name gets updated correctly, but the content the :map field doesn’t get updated, it shows the old value. Which is the main problem I am having.

Marked As Solved

venomnert

venomnert

I was able to resolve the issue, thanks to Ecto’s documentation

A changeset is required as it is the only mechanism for tracking dirty changes. Only the fields present in the changes part of the changeset are sent to the database. Any other, in-memory changes done to the schema are ignored.

Link

Looks like I wasn’t setting the change field for the changeset. So here is the update PromoJob.changeset

 defp add_content(promo_job, attrs) do
    promo_job
    |>put_change(:content,
      Morphix.atomorphiform!(promo_job.data.content)
      |> Content.new()
      |> Content.update_input_value(attrs))

  end

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement