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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:10:18+00:00 2026-05-26T10:10:18+00:00

I have a user, membership and group model. A user has many memberships, and

  • 0

I have a user, membership and group model. A user has many memberships, and groups through memberships. A group has many memberships, and users through memberships. Memberships belongs to both.

When a user accesses the users index view I want to show him/her all users that belong to the group he/she belongs to: I created the following code, which does not work. I cannot seem to get the query to work. Pease help!

The code in the Users controller

class UsersController < ApplicationController

  def index
      @users = current_user.relevant_members
  end

end

The code in the User model

def relevant_members
  group_ids = self.group_ids
  user_ids = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")
  User.find(user_ids)
end

I don’t understand why, when tested in the console, user_ids is not an array of ids, but the following array:

[#<Membership user_id: 2>, #<Membership user_id: 2>, #<Membership user_id: 3>, #<Membership user_id: 3>, #<Membership user_id: 3>, #<Membership user_id: 2>]

What would be a better way of returning an array of user ids to find all relevant group members?

  • 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-26T10:10:18+00:00Added an answer on May 26, 2026 at 10:10 am

    Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")

    returns always an array of Membership objects, select(“user_id”) changes only the fields that are loaded from DB to the object.

    It’s enough to map it to id

    user_ids = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})").map(&:user_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Users model that can have many Groups through Memberships. If a
I have a model Group and model User They are connected both directions with
I have a model with member, group, membership and user. The data is organized
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
I have a basic Users / Memberships / Groups I want a user to
i have two entities one called User and another called Membership which has a
I have a need to design a system to track users memberships to groups
In my Rails app, I have the following objects: Group: has_many users through group_membership
I'm trying to edit the group membership for a user, I have in my
I have a 'User' model which is related to a 'Group' model via HABTM

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.