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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:49:49+00:00 2026-06-11T22:49:49+00:00

I have 3 different Models class GroupMember < ActiveRecord::Base attr_accessible :group_id, :user_id, :owner_id has_one

  • 0

I have 3 different Models

class GroupMember < ActiveRecord::Base
attr_accessible :group_id, :user_id, :owner_id
has_one :user
end

class Group < ActiveRecord::Base
attr_accessible :name, :owner, :permission
has_many :groupMembers
end

class User < ActiveRecord::Base
end

and when im in the groups_controller.rb i want realize the following query with associations

SELECT * FROM groups 
LEFT JOIN  group_members ON groups.id = group_members.group_id 
LEFT JOIN users ON group_members.user_id = users.id WHERE users.id = 1

or the joins query

Group.joins('LEFT JOIN  group_members ON groups.id = group_members.group_id LEFT JOIN users ON group_members.user_id = users.id').where('users.id = ?', current_user.id );

Is this possible?

 Group.joins(:user_id => current_user.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-06-11T22:49:50+00:00Added an answer on June 11, 2026 at 10:49 pm

    I believe it is possible but it is a bit difficult to test as soon as I don’t have a DB with these tables. Probably something like:

    Groups.joins(:group_members, :user).merge(User.where(:id => current_user.id))
    

    But the design is a bit strange. I suppose you want to fetch all groups for a certain user, and it is best put like this:

    @user = User.find_by_id(current_user.id)
    @groups = @user.groups
    

    for this to work you should have groups association in your User model.

    It is also unclear for me why you have GroupMember has_one :user. I understand that group member is related to only one user, but can a user be represented as several group members, of different groups? If yes, the Rails way to design it would be:

    class GroupMember < ActiveRecord::Base
      attr_accessible :group_id, :user_id, :owner_id
      belongs_to :user
      belongs_to :group
    end
    
    class Group < ActiveRecord::Base
      attr_accessible :name, :owner, :permission
      has_many :group_members
      has_many :users, :through => :group_members
    end
    
    class User < ActiveRecord::Base
      has_many :group_members
      has_many :groups, :through => :group_members
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Price < ActiveRecord::Base belongs_to :product end class Metric
I've two models class Article < ActiveRecord::Base has_one :review end class Review < ActiveRecord::Base
I have two models: class Category < ActiveRecord::Base has_one :weight after_create :create_category_weight def create_category_weight
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base
I have three models: class Client < ActiveRecord::Base has_many :balancesheets has_many :investment_assets, :through =>
My models class Team < ActiveRecord::Base has_many :team_players has_many :players, :through => :team_players end
I currently have two different models: User and Project . The User model has
I have a couple of scopes on different (and somewhat linked) models class Alarm
Given two models and a controller: Apples class Apples < ActiveRecord::Base belongs_to :not_oranges ...

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.