I have groups (Group model) in my app, which represent groups of people.
I want each group to have its own forum.
Should I just have the forum id in the groups table? It doesn’t feel right. If I did it myself, the forum would have a polymorphic association to a “forumable” element (groups in this case, but I have other models that would need a forum).
Any opinions on what I should do? Modify the gem to fit my needs, or just have the forum_id in my models that need a forum? Or another solution maybe?
I’m the guy who started Forem (its the volunteers who did most of the hard work, though!), I think I can answer this question.
If you want only certain groups to have access to one and only one forum then you can put the
forum_idfield on thegroupstable and do it that way. What you can do then is override thecan_read_forem_forum?method in yourUsermodel to perform a permission check for that user:This is used in Forem’s ability model to determine whether or not a person can access a forum. What this method is going to do is that it will only return groups for that user that have link that specific forum. If there are any, then it’s known that the user can access that forum.
Now if you’re going the other route where a group may have access to many forums, well then you’d define a joins table between
groupsandforem_forums(calledforum_groups) and define it as an association in yourGroupmodel like this:You would need to also define a new model inside your application for this
forum_groupsassociation, it would be calledForumGroupand go a little like this:We’re doing it this way so you have an easy way to manage the associations between forums and groups. If you did
has_and_belongs_to_many, it generally only provides a gigantic pain in the ass when you want to delete one specific record from that join table.Now, with that all nicely set up, the method you want to define in your
Usermodel is this one:Same thing, except this time we find all the groups that are linked to a specific forum through that association we set up earlier. This will do an
INNER JOINon theforum_groupstable, and then another on theforem_forumstable, getting the data required.I hope this helps you, and thanks for using Forem!