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

  • Home
  • SEARCH
  • 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 320095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:43:19+00:00 2026-05-12T08:43:19+00:00

I have an Account model and a User model: class Account < ActiveRecord::Base has_many

  • 0

I have an Account model and a User model:

class Account < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :account
end

Users belong to an account and an account have a user maximum (different for each account). But how do I validate that this maximum have not been reached when adding new users to an account?

First I tried to add a validation on the user:

class User < ActiveRecord::Base
  belongs_to :account
  validate :validate_max_users_have_not_been_reached

  def validate_max_users_have_not_been_reached
    return unless account_id_changed? # nothing to validate
    errors.add_to_base("can not be added to this account since its user maximum have been reached") unless account.users.count < account.maximum_amount_of_users
  end
end

But this only works if I’m adding one user at a time.

If I add multiple users via @account.update_attributes(:users_attributes => ...) it just goes directly through even if there is only room for one more user.

Update:

Just to clarify: The current validation method validates that account.users.count is less than account.maximum_amount_of_users. So say for instance that account.users.count is 9 and account.maximum_amount_of_users is 10, then the validation will pass because 9 < 10.

The problem is that the count returned from account.users.count will not increase until all the users have been written to the database. This means adding multiple users at the same time will pass validations since the user count will be the same until after they are all validated.

So as askegg points out, should I add validation to the Account model as well? And how should that be done?

  • 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-12T08:43:19+00:00Added an answer on May 12, 2026 at 8:43 am

    If you call account.users.size instead of account.users.count it will also include users which have been built but not saved to the database.

    HOWEVER this will not fully solve your problem. When you call account in a user it is not returning the same account instance that @account is pointing to so it does not know about the new users. I believe this will be “fixed” in Rails 3, but in the meantime I can think of a couple solutions.

    If you are saving the account the same time you are adding users (which I assume so since you are calling update_attributes) then the validation can go in there.

    # in account.rb
    def validate_max_users_have_not_been_reached
      errors.add_to_base("You cannot have more than #{maximum_amount_of_users} users on this account.") unless users.size < maximum_amount_of_users
    end
    

    I’m not sure how you are saving the associated models, but if account validation fails they should not be saved.

    The other solution is to reset the user.account instance to self when updating user attributes. You could do this in the users_attributes setter method.

    # in account.rb
    def users_attributes=(attributes)
      #...
      user.account = self
      #...
    end
    

    This way user’s account will point to the same account instance so account.users.size should return the amount. In this case you would keep the validations in the user model.

    It’s a tricky problem but hopefully this gave you some ideas on how to solve it.

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

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ended up using http://www.xml-rpc.net/ instead of writing my own. May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer Where did you copy it? In Feature and Plug-in custom… May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer You can take advantage of the fact that the command… May 12, 2026 at 10:04 am

Related Questions

I have a View class (OrderView.aspx) which shows the details of an order (Account
I've been playing around with ASP.NET MVC with a site containing a Master Page.
I'm playing with the authlogic-example-app and I'm failing to get the email address from
I am just getting started with MVC, and it seems like it will be
I have a typical User model (username, password, name, etc). I want to allow

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.