Skip to content

Support preloading associations in embedded schemas - #3368

Merged
josevalim merged 1 commit into
elixir-ecto:masterfrom
sebastianseilund:preload-embedded
Aug 5, 2020
Merged

josevalim merged 1 commit into
elixir-ecto:masterfrom
sebastianseilund:preload-embedded

Conversation

@sebastianseilund

Copy link
Copy Markdown
Contributor

Embedded schema structs do not have a __meta__ key, so the old code failed with:

no match of right hand side value: %MyApp.EmbeddedThing{...

If the struct(s) given to preload do not have __meta__, it seems safe to use nil as prefix.


Contrived use case: Post embeds many Comments. Comment (an embedded schema) belongs to an author User.

defmodule User do
  schema "users" do
    field :name, :string
  end
end

defmodule Post do
  schema "posts" do
    embeds_many :comments, Comment
  end
end

defmodule Comment do
  embedded_schema "posts" do
    field :message, :string
    belongs_to :author, User
  end
end

# If I have a `Comment` struct, I'd like to be able to preload its `author
comment = Repo.get(Comment, comment_id) |> Repo.preload(:author)

Related issues: #3238, #1837

Embedded schema structs do not have a `__meta__` key, so the old code failed with:

```
no match of right hand side value: %MyApp.EmbeddedThing{...
```

If the struct(s) given to preload do not have `__meta__`, it seems safe to use `nil` as `prefix`.
@josevalim
josevalim merged commit 5b12dd0 into elixir-ecto:master Aug 5, 2020
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants