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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:06:35+00:00 2026-05-18T06:06:35+00:00

This is mainly a theoritical question, but just wanted to make sure that i’m

  • 0

This is mainly a theoritical question, but just wanted to make sure that i’m doing it correctly. Consider a Ruby Rails project where there are two models, one being User and the other being Alliance.

A user can only have one alliance.
An alliance can have many users.

That is very much simply a :has_many, :belongs_to relationship.

However, i feel that using an intermediate model is a better way to do it. That would be a :has_many :through :users_alliances, where users_alliances would be an intermediate model.

What do you think is the best way to do that ?

  • 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-18T06:06:35+00:00Added an answer on May 18, 2026 at 6:06 am

    Don’t use habtm, it doesn’t make the join into a fully fledged model, which is what you need. Use has_many :through on the alliance side and has_one :through on the user side.

    User
      has_one :alliance_membership
      has_one :alliance, :through => :alliance_membership
    
    AllianceMembership
      belongs_to :user
      belongs_to :alliance
    
    Alliance 
      has_many :alliance_memberships
      has_many :users, :through => :alliance_memberships
    

    Personally i prefer the class name AllianceMembership to UserAlliance but you could use either. UsersAlliances is ugly though imo. (another advantage of has_many/one :through is that you can call the join table/class whatever you want).

    This gives you the option to easily change to users having many alliances later on if you want.

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

Sidebar

Related Questions

This is mainly in regards to my question here , but I don't understand
Mainly this is a question about how to get a similar behaviour as java
I am having trouble with this setup mainly because I am not sure what
This question is mainly about protecting the content inside my iOS app. I intend
This question is mainly to verify my current idea. I have a series of
This question is mainly aimed at shared libraries (.so files) compiled for Linux platforms.
This question and my answer below are mainly in response to an area of
I'm asking this mainly about Java, but I guess it applies to a whole
Hopefully this question is not to confusing, but I can help clear it up
This is mainly a Delphi syntax related question. I need to set a parameter

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.