marick
Many-to-many associations in phoenix and ecto
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a novice, I probably got some things wrong. Please correct me.
http://blog.roundingpegs.com/an-example-of-many-to-many-associations-in-ecto-and-phoenix/
Most Liked
marick
I think I’ve figured it out.
# Create the user. Note that the (empty) `organizations` field has to be preloaded.
user_map = %{display_name: "User 1", login_name: "1@y.com", password: "password"}
changeset = User.password_setting_changeset(%User{}, user_map)
user = Repo.insert!(changeset) |> Repo.preload(:organizations)
# Do the same for the organization:
org = %Eecrit.Organization{short_name: "org1", full_name: "Organization 1"}
org = Repo.insert!(org) |> Repo.preload(:users)
# Update one of the two of them:
changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_assoc(:organizations, [org])
# When you save this change to the user, the join table will have its foreign keys populated in both directions.
Repo.update!(changeset)
Pretty slick.
windholtz
Thats a good start at documenting the many_to_many macro.
If anyone can point the way, I’m looking for some description of how to use many_to_many in CRUD operations with change_sets.
The example you give is the way I am using many_to_many also. But if we need to manage the join table manually like that, I’m not seeing how this is any different that defining has_many on both sides.
Is there a developers guide somewhere that shows usage for the many_to_many macro?
from: Ecto.Schema — Ecto v3.14.0
It gives examples on how to get data back out.
# Get all comments for a given post
post = Repo.get(Post, 42)
tags = Repo.all assoc(post, :tags)
# The comments can come preloaded on the post struct
[post] = Repo.all(from(p in Post, where: p.id == 42, preload: :tags))
post.tags #=> [%Tag{...}, ...]
But what I’m completely stumped about is how to add, update, and delete data from a many_to_many.
josevalim
Hi everyone, sorry for being late to the discussion. If the documentation is incomplete, pull requests are always welcome! I have already improved those examples in the docs though. ![]()
I have also removed the restriction that associated entries need to be changesets. We will now convert them implicitly.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









