Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:27:18+00:00 2026-05-14T14:27:18+00:00

I have a need to design a system to track users memberships to groups

  • 0

I have a need to design a system to track users memberships to groups with varying roles (currently three).

class Group < ActiveRecord::Base
 has_many :memberships
 has_many :users, :through => :memberships 
end

class Role < ActiveRecord::Base
 has_many :memberships
 has_many :users, :through => :memberships
end

class Membership < ActiveRecord::Base
 belongs_to :user
 belongs_to :role
 belongs_to :group
end

class User < ActiveRecord::Base
 has_many :memberships
 has_many :groups, :through => :memberships
end

Ideally what I want is to simply set

@group.users << @user 

and have the membership have the correct role. I can use :conditions to select data that has been manually inserted as such :

:conditions => ["memberships.role_id= ? ", Grouprole.find_by_name('user')]

But when creating the membership to the group the role_id is not being set.

Is there a way to do this as at present I have a somewhat repetitive piece of code for each user role in my Group model.

UPDATED

It should be noted what id ideally like to achieved is something similar to

@group.admins << @user
@group.moderators << @user

This would create the membership to the group and set the membership role (role_id ) appropriately.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:27:18+00:00Added an answer on May 14, 2026 at 2:27 pm

    You can always add triggers in your Membership model to handle assignments like this as they are created. For instance:

    class Membership < ActiveRecord::Base
      before_save :assign_default_role
    
    protected
      def assign_default_role
        self.role = Role.find_by_name('user')
      end
    end
    

    This is just an adaptation of your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which is going to need to use the strategy design
We currently have a custom inventory system used to track different assets. The items
I often design system where I have a class in my system that has
We need to design a system which allows users to search by different keywords
I have existing java code and need to create Design Document based on that.
We have need for a rating system in a project we are working on,
I need to design a system which has these basic components: A Webserver which
What is the best way to design a system where users can create their
I need to design a thread-safe logger. My logger must have a Log() method
I've been asked to design an alarm system that would allow some users to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.