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 6666671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:51:11+00:00 2026-05-26T02:51:11+00:00

My current Group model: class Group < ActiveRecord::Base has_many :memberships, :dependent => :destroy has_many

  • 0

My current Group model:

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

My Current User Model

 class User < ActiveRecord::Base
      has_and_belongs_to_many :roles
      has_many :memberships, :dependent => :destroy
      has_many :groups, :through => :memberships
      #some more stuff
    end 

Membership Model

class Membership < ActiveRecord::Base
  attr_accessible :user_id, :group_id
  belongs_to :user
  belongs_to :group 
end

Role Model

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

I have a Ability class and CanCan installed to handle roles. I have a role type groupleader and need to make sure a Group has only one groupleader…

I think its something like: Group has_one User.role :groupleader… but I know thats not it.

  • 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-26T02:51:11+00:00Added an answer on May 26, 2026 at 2:51 am

    It doesn’t make sense to me to have the role on the users table if you want it to determine what the user can do within the context of a group.

    Where it would make sense is to have it on the memberships table for groups and users. Records in this table would then have three columns: user_id, group_id and role.

    Then to retrieve the leader for the group you would execute a query like this:

    group.users.where("memberships.role = 'leader'").first
    

    Where group is a Group object, i.e. Group.first or Group.find(13).

    This then leaves open the possibility that you can have more than one leader for a group further down the track if required.


    If your roles are in a separate table, then you can do this:

    group.users.where("memberships.role_id = ?", Role.find_by_name("leader").id).first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have the models User and StoredItem: class UserData < ActiveRecord::Base has_many :stored_items, :dependent
I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class
I have a user, membership and group model. A user has many memberships, and
Using my c++ program how can I find out what group the current user
I have a current association where: Group :has_many Employees and Employee :belongs_to Group but
My Rails application have a User model and a Group model, where User belongs
So I have 2 models. Users and Groups. Each group has a user as
Trying to figure out an equation to get the current group a page would

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.