venomnert

venomnert

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.

Showing Posts 1 to 1

venomnert

venomnert OP

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews