f0rest8

f0rest8

New Ecto "How-To" Guide: Self-referencing many to many

Hi everyone,

Just wanted to say that the new Self-referencing many to many guide is now up on the official Hex docs (at least I just noticed :blush:). If anyone is thinking about (or struggling with) implementing associations that self-reference each other, then I encourage you to check out this guide.

This latest guide is inspired from my own utilization of this association strategy in an upcoming social connection web app built with Elixir and Phoenix — it was forged in fire, so to speak. So, I hope it helps anyone else struggling to implement a similar strategy in their own applications. :heart:

Most Liked

cs44

cs44

I’m following your guide, which is great - thanks, but am left wondering how you might handle adding multiple records vs a single call to the Relationship changeset.

f0rest8

f0rest8

I would use Ecto.Multi, something like:

Ecto.Multi.new()
|> Ecto.Multi.insert(table_name, changeset(struct, attrs))
|> Repo.trasaction()

# So, with the 'relationship' naming:
Ecto.Multi.new()
|> Ecto.Multi.insert(
  :relationships,
  Relationship.changeset(%Relationship{}, attrs)
)
|> Repo.transaction()

If I’m not mistaken, what’s great about it is that you don’t really have to do anything tricky. I haven’t tested this but I think this is a good guide on Ecto.Multi from Elixir School.

** Forgot to include Repo.transaction() :blush:

Where Next?

Popular in Guides/Tuts Top

sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
bentanweihao
I wrote a thing: http://engineering.pivotal.io/post/how-to-set-up-an-elixir-cluster-on-amazon-ec2/ Hope this is helpful!
New
pinksynth
Whenever tests have to interact with an Ecto.Repo, sometimes it’s necessary to test a few different branches or paths that data can take....
New
emilsoman
Hello, While answering a StackOverflow question on how to debug an elixir node running remotely, I thought it may be helpful to write a...
New
lukertty
Install web-mode and mmm-mode first and put this in your config file: (require 'mmm-mode) (require 'web-mode) (setq mmm-global-mode 'may...
New
caspg
Hi everyone, I recently implemented a real-time search feature in a Phoenix application using LiveView and Tailwind, and I wanted to sha...
New
stryrckt
I’m excited about the new LiveComponents feature in LiveView, but I haven’t seen much written on it, so I decided to write a couple of ar...
New
AstonJ
This blog post hit my timeline earlier, and I’ve also been learning about some fantastic Elixir related tips via @pragdave’s new online c...
New
KoviRobi
Hi, I’ve written the following to debug function calls, not sure if it’s useful for anyone else, and if so should I put it somewhere? i...
New
AstonJ
With a few questions relating to this recently, I wonder if it might help having a thread where we share how we’ve set up Elixir/Erlang/P...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement