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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:03:26+00:00 2026-06-07T14:03:26+00:00

If I have a has_and_belongs_to_many relationship between two models, let’s say Users and Accounts

  • 0

If I have a has_and_belongs_to_many relationship between two models, let’s say Users and Accounts, can I require that a User have at least one Account, and how?

Also, using the has_and_belongs_to_many relationship, is it possible for an Account not to have a User?

What I need is a relationship where Accounts can live on their own, and belong to Billers, but they can also belong to Users if a User signed up with one. Is this possible, and how?

  • 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-07T14:03:27+00:00Added an answer on June 7, 2026 at 2:03 pm

    I personally would drop the the HABTM. Instead I would use has_many :though=>

    You would need to create two new models, account_users, and account_billers. You likely already have join tables for the HABTM, but this will expose them as models so they will need ID fields.

    So you would end up with something like the following:

    class Account < ActiveRecord::Base
      has_many :account_billers
      has_many :account_users
    
      has_many :billers, :through=> :account_billers
      has_many :users, :through=> :account_users
    end
    
    class User < ActiveRecord::Base
      has_many :account_users
      has_many :accounts, :through=>:account_users
    
      validates :accounts, :length => { :minimum => 1}
    end
    
    class Biller < ActiveRecord::Base
      has_many :account_billers
      has_many :accounts, :through=>:account_billers
    
      validates :accounts, :length => { :minimum => 1}
    end
    
    class AccountUser < ActiveRecord::Base
      belongs_to :user
      belongs_to :account
    end
    
    class AccountBiller < ActiveRecord::Base
      belongs_to :biller
      belongs_to :account
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a many-to-many relationship between users and group. A user can
Say I have two models, Book and Author with a has_and_belongs_to_many relationship between them.
I have the following two models: School and User, and a HABTM relationship between
I have two models, Project and Category, which have a many-to-many relationship between them.
So I recently created an HABTM relationship between two models (project & user). Before,
I had a many-to-many relationship between two Django Models: from django.contrib.auth.models import User class
I have two tables, users and groups . A user can belong to many
I have a table joining two models in a has many through relationship. Users
I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at
I have multiple belongs_to relationships to the same model. Modeling messages between two users

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.