thojanssens1

thojanssens1

Preload associations in embeds?

Say I have the following schemas:

defmodule Articles do
  schema "articles" do
    field(:name, :string)
    embeds_one(:foo, Foo)
  end

  def changeset(struct, attrs) do
    struct
    |> cast(attrs, [:name])
    |> cast_embed(:foo)
  end
end
defmodule Foo do
  @primary_key false

  embedded_schema do
    field(:name, :string)
    belongs_to(:country, Country)
  end

  def changeset(struct, attrs) do
    struct
    |> cast(attrs, [:name])
    |> cast_assoc(:country)
  end
end
defmodule Country do
  schema "countries" do
    field :name, :string
  end
end

If I try to run the code below, I’ll have the error:

** (RuntimeError) attempting to cast or change association country from Foo that was not loaded. Please preload your associations before manipulating them thr
ough changesets

attrs = %{
  name: "An article",
  foo: %{
    country: %{name: "Fiji"}
  }
}

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

If I try to preload:

%Article{}
|> Repo.preload([foo: :country])

I’ll have the error:

** (ArgumentError) schema Article does not have association :foo

First Post!

LostKobrakai

LostKobrakai

You cannot have associations in embeds, as this is not something databases support.

Last Post!

BradS2S

BradS2S

Great news! Associations work inside of embeds. You have a small typo.

:country should be :countries. :slight_smile:

I compiled it and it ran fine.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

sergio_101
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
albydarned
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
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement