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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:01:40+00:00 2026-06-18T11:01:40+00:00

I have two tables, users and groups . An user owns a group and

  • 0

I have two tables, users and groups. An user owns a group and can be apart of multiple groups. A group belongs to one user and can have many users.

Thus for my user model I have

has_and_belongs_to_many :groups
has_many :groups

While for my group model I have

has_and_belongs_to_many :users
belongs_to :user

I also have a join table in my migrations..

def change
  create_table :groups_users, :id => false do |t|
    t.integer :group_id
    t.integer :user_id
  end
end

My question is does this make sense? I feel like I’m doing something wrong by having has_many and belongs_to on top of has_and_belongs_to_many.

  • 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-18T11:01:41+00:00Added an answer on June 18, 2026 at 11:01 am

    The way I would approach this, and this is my own personal methodology, is with 3 tables/models like so:

    group_user.rb

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

    group.rb

    class Group < ActiveRecord::Base
      attr_accessible :owner_id
    
      validates_presence_of :owner_id
    
      has_many :group_users
      has_many :users, through: :group_users
    end
    

    user.rb

    class User < ActiveRecord::Base
      attr_accessible :some_attributes
    
      has_many :group_users
      has_many :groups, through: :group_users
    end
    

    Then, whenever you create a Group object, the User that created it would have its id placed in the owner_id attribute of Group and itself into the GroupUser table.

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

Sidebar

Related Questions

I have two tables, users and groups . A user can belong to many
I have two models, Users and Groups. Each group can have many users and
I have two tables, one called Users and one called Groups . I also
I have two database tables, grouptypes and groups. A grouptype can have multiple groups,
I have two tables user (one) and transaction (many) and I need to get
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I have a PostgreSQL database with two tables named user and group. They have
I have two tables, one that stores user earnings and another that stores the
If I have users, as well as user groups (like a local astronomy group/club),
I have two tables: Table groupMessage is used for record group user Messages Table

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.