silverdr
In a many-to-many relationship, let’s say users and chatrooms for a good example, there’s a join table users_chatrooms and appropriate Ecto.Schema entries:
many_to_many :users, PhxApp.Accounts.User, join_through: PhxApp.Accounts.UserChatroom
and
many_to_many :chatrooms, PhxApp.Chatrooms.Chatroom, join_through: PhxApp.Accounts.UserChatroom
but let’ say I need to store additional information like whether given user is the “owner” or a “moderator” of a given chatroom and the obvious place to store this kind of info is on the join table. So I have a role_id column on that table meant to store this information. Now, how does one do the CRUD stuff in such setup? Let’s say in the simplest case first - the user who creates the chatroom is always the owner but the role_id has to be set explicitly rather than being a DB default?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
It’s totally doable… but not with many_to_many.
With two has_many, and one should be has_many through.
silverdr
I see. Any suggested readings (other than Ecto docs) on how does one do such things “properly” in Phoenix (Ecto)?
LostKobrakai
You can find some docs here: Ecto.Schema — Ecto v3.7.1
I blogged about a bit of the background to why things are the way they are:
silverdr
Tnx. I found your blog post already before and it gives a good background but am looking for something lower level. More of “how does one work with it”. I know the Ecto docs are there so explicitly excluded them in the question
Found also this thread: Ecto associations and the purpose of has many through and many to many But still - do I need to take low level care of things (transactional creating of join records for example like the OP there does) or are there some built-in mechanisms / “best practices” that I’d be best aware of before I create lots of completely unneeded code 
LostKobrakai
Can you explain how your insert/update code looks like right now?
silverdr
It doesn’t [exist yet]. I first modelled the relations using
has_many, then I realised that I had no idea how to store extra data on the join table. I found some bits about usinghas_many throughinstead but still asked here, seeking either confirmation or some light-shed. Now that you guys confirmed thatmany_to_manyis not the way to go, I want to continue and write what you asked about. Preferably being equipped with some “correct” examples to look up to.LostKobrakai
So if there’s no code: Do you know how you receive the data to be inserted? If there’s no code yet you can almost ignore the fact that there is a join table between users and chatrooms. You can just model it as
User <> ChatroomUser <> Chatroom. Ecto will probably take care of the rest if relationships between schemas are setup.silverdr
In the simplest example of “create” I receive the chatroom metadata (like name for example) from the web form. Since the user creating it will be the owner, this role needs to be assigned when creating the join record. But in the linked thread the OP creates the join record “manually” within a
Multitransaction. Sure that will work but is this what one has to do?LostKobrakai
So the user already exists. So essentially we don’t need to handle the many to many anymore to begin with given you’re not actually affecting both ends. All you insert is the chatroom and the first user of it:
One important thing ecto doesn’t do, but people somehow expect is having an API to create relationships to already existing records. Just set the
:record_idon wherever you need it. There isEcto.build_assoc, but usually it doesn’t make things simpler.silverdr
Yes, that’s the simplest use case. There will be other, like “a non-yet-existing user has been invited to join the chatroom via e-mail” and then both the user and the chatroom association need to be created with a specific role set by the inviting user. In such case though I find it more “okay” to create the user first and then the rest. Or to manually build a transaction around this process. I somehow found it hard to believe that I model the associations and then still have to go that low level even in the simplest case
TNX. And I don’t need to build a transaction, do I? All will be taken care of, right?