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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement