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?
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
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 using has_many through instead but still asked here, seeking either confirmation or some light-shed. Now that you guys confirmed that many_to_many is 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.
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.
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 Multi transaction. Sure that will work but is this what one has to do?
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_id on wherever you need it. There is Ecto.build_assoc, but usually it doesn’t make things simpler.
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?