tankh7

tankh7

So this is my first time using embedded schema and I’m having an issue finding information to help me work through how to handle what I need to do in my app.

Basically, a company has the option to white label their site. We’re storing their white label configurations in an embedded schema that we can pull into our front-end applications.

As of now we’re storing the white label information in a white label table in our database and each configuration belongs to a company. I’m unsure of how to handle creating and updating the embedded schema.

defmodule App.WhiteLabels.WhiteLabel do
  @moduledoc false

  use Ecto.Schema
  import Ecto.Changese

  schema "white_labels" do
    field(:subdomain, :string)
    belongs_to(:company, App.Companys.Company
      foreign_key: :company_id
    )
    embeds_one(:themes, App.WhiteLabels.Theme)
    timestamps()
  end

  @doc false
  @spec changeset(%__MODULE__{}, map) :: %Ecto.Changeset{}
  def changeset(schema, attrs) do
    schema
    |> cast(attrs, [:subdomain, :company_id])
    |> validate_required([:subdomain, :company_id])
    |> cast_embed(:themes, required: true)
  end
defmodule App.WhiteLabels.Theme do
  @moduledoc false

  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :logo, :string
    field :logo_alt, :string
    field :favicon, :string
    field :background, :string
    field :header_background, :string
    field :header_text, :string
    field :title, :string
    field :text, :string
  end

  @fields [
    :logo,
    :logo_alt,
    :favicon,
    :background,
    :header_background,
    :header_text,
    :title,
    :text,
  ]

  @doc false
  @spec changeset(%__MODULE__{}, map) :: %Ecto.Changeset{}
  def changeset(schema, attrs) do
    schema
    |> cast(attrs, @fields)
  end
end

How would I create a new white label when I need to create %WhiteList{} and %Theme{} at the same time? Does this make sense? Most of the examples I find are adding new embedded schema data to already created data or updating an embedded schema.

Showing Posts 1 to 8

thojanssens1

thojanssens1

Not an Ecto expert:) So I guess somewhere in your Context you have:

%WhiteLabel{}
|> WhiteLabel.changeset(attrs)
|> Repo.insert()

and you send the “themes” data alongside the subdomain. Doesn’t that work? What problem are you encountering? What is the structure of the data you are sending to the WhiteLabel changeset function (can you IO.inspect it)?

Sanjibukai

Sanjibukai

Hi,

In my personal current learning path, I don’t yet reach advanced topic like embedded schema (and I even didn’t hear of it until now).

But I already had to deal with similar concern and JSON (or JSONB) field in Postgres works like a charm.
But I admit that (at least when I did it in rails) it was painful to do validation (and even casting values)
IIRC latest version of Rails and Active Records allow virtual attributes but anyway it was somehow difficult to maintain.

But I guess (I mean I hope) that Ecto Changeset can deal easily with store fields in Postgres thanks to cast, or I might be wrong..

BTW interesting to see how you’ll deal with that.
Good luck in the meantime.

dimitarvp

dimitarvp

I think you should correct :themes to :theme. Since you are using plural and you are not supplying extra options then Ecto could be tripped up.

tankh7

tankh7 OP

You’re right, currently I have the following when creating a White Label:

    %WhiteLabel{}
    |> WhiteLabel.changeset(attrs)
    |> Repo.insert()

With what you’re saying I should be fine passing in attrs that look like the following:

    attrs = %{
      company_id: 1,
      subdomain: "acme-corp",
      theme: %{
        logo: "https://image.logo.jpg"
        logo_alt: "Company Logo",
        favicon: "https://image.favicon.favicon.jpg",
        background: "https://image.background.jpg",
        header_background: "#212121",
        header_text: '#FFF',
        title: '#212121',
        text: '#212121'
      }
    }
thojanssens1

thojanssens1

So is it :theme or :themes? The schema field says :themes and in your attrs I see :theme.
And what is the error/result?

tankh7

tankh7 OP

I am going to change :themes to :theme I think as per the comment by @dimitarvp

Sorry I meant to add that in my reply. I’m going to run this now and see what result I get.

tankh7

tankh7 OP

Works well!

Create white label:

    attrs = %{
      company_id: 1,
      subdomain: "acme-corp",
      theme: %{
        logo: "https://image.logo.jpg"
        logo_alt: "Company Logo",
        favicon: "https://image.favicon.favicon.jpg",
        background: "https://image.background.jpg",
        header_background: "#212121",
        header_text: '#FFF',
        title: '#212121',
        text: '#212121'
      }
    }

    %WhiteLabel{}
    |> WhiteLabel.changeset(attrs)
    |> Repo.insert()
dimitarvp

dimitarvp

Glad to hear you made it work.

Let’s be clear: you are free to use whatever names and break conventions – but you’ll have to specify more attributes (configuration) if you do so. That’s why your initial code didn’t work – you didn’t give Ecto the extra configuration options that it expected since your name implied one-to-many association.

You can take a look at Ecto.Schema docs and take it from there (for the future).

Also consider marking one of the comments (yours included) as an accepted answer. It will help future readers.

— 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
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
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
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews