views:

217

answers:

2

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?

A: 

The reason it is passing is because update_attributes does not go through validations.

Also - your logic only checks for the existing number of account against their maximum permitted. There is no calculation considering the number of users attempting to be added. I would think this logic belongs more in the Account model (?).

askegg
update_attributes does run validations - at least in Rails 2.3.2.But the problem is that the validation method on the User model validates that the max is not reached compared to how many users that is already on the account. I.e. if max == 10 and users.count == 9, then - when adding two users at the same time - the validations are run for both of them but each time validating that 9 < 10. Since users.count will not increase before the users are written to the database.But how would one validate that on the Account model? Should I override the update_attributes etc.?
Thomas Watson
Ahh - excuse me. Update_attribute (note the missing "s" on the end) bypasses validations. My mistake.Looks like Ryan (who has much more Rubyfoo than I) has answered your question. I did not know the "size" method includes unsaved records. You learn something every day :)
askegg
+3  A: 

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.

ryanb