rethinkdb_ecto icon indicating copy to clipboard operation
rethinkdb_ecto copied to clipboard

Associations

Open hamiltop opened this issue 9 years ago • 3 comments

I spent a bit of time playing with associations.

Definite steps forward: Use Ecto to manage associations. has_one, has_many, belongs_to, embeds_one, embeds_many, etc. are all very well built and easy to inspect at runtime.

Uncertainty: How do we read and write?

Ecto does preloading on reads it via Ecto.Query. We would need an alternative approach.

Read

I see two approaches that could provide this functionality.

  1. Provide wrappers for the Schemas to include the preloaded data:

Repo.get!(Post |> preload([:comments]), id)

  1. Accept an option for to include the preloaded data:

Repo.get!(Post, id, preload: [:comments])

Writes

Ecto.Changeset can handle all the writes just fine. Insert and Update should work exactly like ecto.

hamiltop avatar Feb 13 '16 21:02 hamiltop

Purely on syntax, I prefer the second option - I don't know how I feel about a pipe operator inside an argument.

martimatix avatar Feb 13 '16 22:02 martimatix

@martimatix Repo.get!(preload(Post, [:comments]), id) would work just as well.

hamiltop avatar Feb 13 '16 22:02 hamiltop

From slack:

Post |> preload([:comments]) |> Repo.query(query_with_join)

I actually do like that.

hamiltop avatar Feb 13 '16 22:02 hamiltop