How do I allow the owner of a group add new moderators?
Hi, I am trying to develop similiar functionallity to Shopify where the owner of the store can add new managers to the seller account and allow them to manage orders. However, in my case its just users
and groups
.
Currently, I have a working form for users where they can create a new group.
# User model
class User < ApplicationRecord
...
has_one :group, dependent: :delete, foreign_key: :owner_id
...
end
# Group model
class Group < ApplicationRecord
belongs_to :owner, class_name: "User"
end
After submit
, the user's id moves to the owner_id
column in groups
table. Please note, the user can have only one group. If the user is the owner, he won't be able to become a moderator, the same approach applies to the moderators.
I tried to google and found couple answers:
- YouTube video - Rails API: Multiple Accounts and Friendly ID
- Stackoverflow - Adding an “Account” that has many “Users”
- Stackoverflow - Adding multiple users into a single record Rails 5
Not sure if I am doing everything right, but I came up with the idea to create join table group_moderators
with user_id
and group_id
:
# Group Moderator Model
class GroupModerator < ApplicationRecord
belongs_to :user
belongs_to :group
end
Updated version of group
model:
class Group < ApplicationRecord
belongs_to :owner, class_name: "User"
has_many :group_moderators
has_many :users, through: :group_moderators
end
What am I doing wrong? How do I allow the owner of the group invite moderators? At the beginning I simply wanted to update group_id
column for the invited user but in this case my group
controller doesn't know about the invited user since it doesn't have any information in the groups
table.
Thank you very much for your help and time.
PS: Not sure if it matters but in the future, the owner will be able to give the rights to each moderator.
Maybe this can help you https://gorails.com/episodes/inviting-users-with-devise_invitable?autoplay=1