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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:16:16+00:00 2026-05-10T22:16:16+00:00

Say that I have two models- Users and Accounts. Each account can have at

  • 0

Say that I have two models- Users and Accounts. Each account can have at most n users associated with it, and a user can only be associated with one account.

It would seem natural to say that User

belongs_to :account 

and Account

has_many :users 

However, I’m not clear on the best practice when it comes to limiting the number of associations through that has_many declaration. I know that there is a :limit argument, but that that only limits the number of associations returned, not the number that are able to exist.

I suspect that the answer is to use something like :before_add. However, that approach seems only to apply to associations created via << . So it would get called when you used

@account.users << someuser  

but not if you used

@account.users.create 

I had also considered that it might be more practical to implement the limit using before_save within the User model, but it seems like it would be a bit off to implement Account business rules within the User model.

What is the best practice for limiting the number of associations?

Edit: the n users per account would be some business data that is stored within the individual accounts, rather than being a straight up magic number that would be floating around willy nilly in the code.

  • 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. 2026-05-10T22:16:16+00:00Added an answer on May 10, 2026 at 10:16 pm

    At first, if your users table has foreign key account_id then you need to use

    class User   belongs_to :account end 

    In this way you will ensure that User can be associated just to one Account.

    If you want to limit that Account can have e.g. at most 3 users then you can define the following validation:

    class User   validates_each :account do |user, attr, value|     user.errors.add attr, 'too much users for account' if user.account.users.size >= 3   end end 

    and as a result you will not be able to create new user for account if account has already 3 users.

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

Sidebar

Related Questions

Say I have two entities in Doctrine2 that are related to each other, Models\User
Let's say that I have two libraries (A and B), and each has one
If I have a has_and_belongs_to_many relationship between two models, let's say Users and Accounts
Say that I have two figures in matplotlib, with one plot per figure: import
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I'm trying to get my head around this one: Say you have two models
Let's say that I have two tables in my database: users countries I have
I have two models that are associated. I have an Artist model and a
Say that you have an application where different kind of users can sign: Firms,
I'm working on an app where a user can have one of two kinds

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.