tankh7
Best way to handle defaults for embedded schema
I have a schema with an embedded schema.
When a company is first created I want to give them defaults for this schema and embedded schema that they can later change.
schema "white_labels" do
field(:subdomain, :string, default: "")
belongs_to(:company, App.Companies.Company,
foreign_key: :company_id
)
embeds_one(:theme, App.WhiteLabels.Theme) #would like the keys in this map to have defaults
timestamps()
end
embedded_schema do
field(:logo, :string, default: "https://images.unbranded-logo.png")
field(:favicon, :string, default: "https://images.unbranded-favicon.png")
field(:background, :string, default: "https://images.unbranded-background.png")
field(:header_background, :string, default: "https://images.unbranded-header-background.png")
field(:header_text, :string, default: "#FFF")
field(:title, :string, default: "#212121")
field(:text, :string, default: "#212121")
field(:primary, :string, default: "#5CB85C")
field(:primary_text, :string, default: "#FFF")
field(:secondary, :string, default: "#FDD835")
field(:secondary_text, :string, default: "#2C2C2C")
field(:tertiary, :string, default: "#003260")
field(:footer_text, :string, default: "#FFF")
field(:virtual_card_text, :string, default: "#6F808B")
end
When my changeset is like the below I get an error saying :theme is required, as expected
def create_changeset(schema, attrs) do
schema
|> cast(attrs, [:subdomain, :company_id])
|> validate_required([:company_id])
|> cast_embed(:theme, required: true)
end
However if I have my changeset as this below I get nil returned for :theme
def create_changeset(schema, attrs) do
schema
|> cast(attrs, [:subdomain, :company_id])
|> validate_required([:company_id])
|> cast_embed(:theme)
end
What’s the best way to implement defaults for an embedded_schema.
Most Liked
brucepomeroy
I think you could also do something like this:
put_embed(changeset, :theme, %App.WhiteLabels.Theme{})
Note use of put_embed rather than cast_embed for this scenario.
3
tankh7
Figured this out by passing in an empty map for the :theme key when creating a company
If there are other suggestions I’d love to hear them!
1
Popular in Questions
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
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
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
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
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
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
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
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New








