Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I allow the owner of a group add new moderators?

Clyro asked in Rails

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:

  1. YouTube video - Rails API: Multiple Accounts and Friendly ID
  2. Stackoverflow - Adding an “Account” that has many “Users”
  3. 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.

Reply
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.