tankh7

tankh7

Help with how to use schema and embedded schema

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.

Marked As Solved

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)?

Also Liked

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

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.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement