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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:32+00:00 2026-05-13T13:59:32+00:00

I have two tables, users and groups . A user can belong to many

  • 0

I have two tables, users and groups. A user can belong to many groups. A group can have many users.

So I have created a have_and_belongs_to_many relationship between users and groups using a join table, groups_users. This all works as expected.

What I would also like to do is specify an ACTIVE group for each user. Were it not for the habtm relationship I have already defined, I would create a column “group_id” in users for the active group, and then I would define a one-to-many relationship between the models as follows:

class User < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :users
end

This didn’t work. I could not access group properties like “@user.group.name”. I suspect that I’m asking too much of Rails by specifying two relationships.

So I have three questions.

  1. I could very easily understand if combining the two relationships confuses Active Record . Is that the case?
  2. If so, how would you implement these relationships? Right now I’m just manually using the group_id, but that feels messy.
  3. Regardless of whether I am using Active Record magic or manually setting the active group, it is possible for a user’s active group to be outside the group’s they belong to using the first habtm relationship. Any thoughts on how to implement this with the constraint that the active group must be a group that the user belongs to?

Thanks for any insights. I am a couple of weeks into the Rails learning curve and I think that getting to the bottom of this little problem will deepen my understanding of models and table relationships quite a bit.

  • 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-13T13:59:33+00:00Added an answer on May 13, 2026 at 1:59 pm

    Greg – I’ve done this before, similar to what as your proposing. I used a “primary_group” instead of “group” and set the foreign_key to be “primary_group_id” and the :class_name => ‘Group’

    I don’t use has_and_belongs_to_many very often so I don’t know if that would be causing an issue, but I don’t think so – at least not with the user model. You don’t have a #group method, so setting belongs_to should be OK. I don’t know if you absolutely must have have a has_many :users method on the group, but you may not need it anyway. This will certainly cause a collision with the ‘users’ method that habtm creates. If you do need this, try has_many :primary_users and set the :foreign_key => :primary_group_id

    Just some thoughts. You should be able to do what you’re doing now, so not sure what’s failing.

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

Sidebar

Related Questions

I have two models, Users and Groups. Each group can have many users and
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I am using EF4 with the EDM designer. I have two tables : Users
I have two entity groups. Restaurants and Users. Restaurants can be rated (1-5) by
I have two tables, one called Users and one called Groups . I also
I have a PostgreSQL database with two tables named user and group. They have
If I have users, as well as user groups (like a local astronomy group/club),
I have three tables: User -- contains users Group -- contains a set of
I have two tables user (one) and transaction (many) and I need to get
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.