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

  • Home
  • SEARCH
  • 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 5928295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:16:33+00:00 2026-05-22T14:16:33+00:00

In my app, a Person can have a Group , and a Group can

  • 0

In my app, a Person can have a Group, and a Group can be owned by just one person. But persons can be part of groups too, not as owners, but as members. A Group can have more than one member. So to a person become a member, she/he needs to ask for Group_Membership. This way, a group_membership model is created, with a boolean status with false as default. The Group owner than let the member in by changing the status to true. Here are the models:

class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end

class Group_Membership
belongs_to :member, :class_name => 'Person'
belongs_to :group
scope :asked, where(:status => false)
end

class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"

What I need is to display in the person#show the groups that the person is in, and as well the requests that the person got to the groups owned by him/her.

def show
@person = Person.find(params[:id])
@asked_membership = @person.group_memberships.asked.where(:member_id => params[:id]) 
@group = @person.groups.where(:person_id => params[:id])
@asked_group = @person.group_memberships.asked.where(:group_id => params[@group])
end

With that I’m able to see the groups that a person is in, even though I set my status in scope as false to test (since status is false by default should show all the requests made by the person). This is the view:

    <% @asked_membership.each do |group_membership| %>
    <%=h group_membership.member_id %> 

How can I can display using this view the name of the groups instead of member_id?

And @asked_group is the right query for me to get the all the requests that were made from other persons to join the group owned by the person in question? If so, (:group_id => params[@group]) is always getting null value and I don’t know how to repair it.

Thanks in advance.

##EDIT##

Changing @asked_group to @asked_group = @person.group_memberships.asked.where(:group_id => @group.id) and adding this to the Group Controller made it work. Now, still need to display name of the groups instead of member_id.

  • 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-22T14:16:33+00:00Added an answer on May 22, 2026 at 2:16 pm

    I would rewrite your models as follows:

    class Person
      has_many :owned_groups, :class_name => "Group", :foreign_key => :owner_id
      has_many :owned_group_memberships, :through => :owned_groups, 
                  :source => :group_memberships 
    
      has_many :group_memberships, :foreign_key => "member_id"
      has_many :groups, :through => :group_memberships
    
      has_many :approved_groups, :through => :group_memberships, :source => :group, 
                   :conditions => [ "group_memberships.status = ? ", true]
    
      has_many :applied_groups, :through => :group_memberships,, :source => :group, 
                   :conditions => [ "group_memberships.status = ? ", false]
    
    end
    
    class GroupMembership
      belongs_to :member, :class_name => 'Person'
      belongs_to :group
    end
    
    class Group
      belongs_to :owner, :class_name => "Person"
      has_many :group_memberships
      has_many :members, :through => :group_memberships
    
      has_many :approved_members,:through => :group_memberships, :source => :member, 
                   :conditions => [ "group_memberships.status = ? ", true]
    
      has_many :applied_members,:through => :group_memberships, :source => :member, 
                   :conditions => [ "group_memberships.status = ? ", false]
    
    end
    

    Now given a person:

    # returns all the groups is user is member of
    person.groups
    
    # returns all the groups owned by the user                  
    person.owned_groups                
    
    # returns the memberships of all the groups owned by the user
    person.owned_group_memberships 
    
    # returns the memberships requests for all the groups owned by the user
    person.owned_group_memberships.find_all_by_status(false)
    

    You can further optimize the results by eager loading the group and member:

    @asked_group_memberships=person.owned_group_memberships.find_all_by_status(false,
      :include => [:group, :member])
    

    In your view:

    <% @asked_group_memberships.each do | agm| %>
      <p> 
          Requester: <%= agm.member.name %>, 
          Group: <%=agm.group.name%> 
      </p>
    <%end %>
    

    To check if a person is a member of a group:

    person.groups.exists?(group)
    

    To check if a person is a member of a approved group:

    person.approved_groups.exists?(group)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app where users can follow one another. I've just discovered
In my Rails 3 app, a person's profile can have superlatives. In fact, the
In my Rails app Users can have many People which in turn can (but
I am currently building a Rails app where a User can have many Persons
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
I have a client that wants an app that will detect the persons location
We have a web app which can upload files to S3. For this to
I am developing an app that needs to match people together. Each person can
i have a mini app where users can login, view their profile, and follow
Say I have the following ember-data model: App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'),

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.