验证最大金额AF相关联的对象 [英] Validate max amount af associated objects

查看:133
本文介绍了验证最大金额AF相关联的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个帐户模式和用户模式:

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.

如果我通过添加多个用户@ account.update_attributes(:users_attributes =&GT; ...)它只是直接通过,即使有一个更只有房用户。

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.

更新:

只是为了澄清:目前的验证方法,验证了 account.users.count 小于 account.maximum_amount_of_users 。所以说,例如该 account.users.count 是9和 account.maximum_amount_of_users 10,则验证将通过因为9&LT; 10。

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.

的问题是,从 account.users.count 返回的计数将不会增加,直到所有的用户都被写入到数据库中。这意味着在同一时间将通过验证,因为用户数将是相同的,直到它们都被验证之后加入多个用户。

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.

那么作为的 askegg 的指出,我应该添加验证帐户模式呢?应当如何的做?

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

推荐答案

如果你调用 account.users.size 而不是 account.users。计数这也将包括其中已建成但尚未保存到数据库中的用户。

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.

然而这不能完全解决你的问题。当你调用帐户在用户它没有返回相同的帐户实例 @account 指向它确实是这样不知道新的用户。我相信,在Rails 3中,这将是固定的,但在此期间,我能想到的一对夫妇的解决方案。

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.

如果要保存的帐户要添加用户(我假设如此以来您所呼叫 update_attributes的),那么验证可以去那里的同时。

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.

另一种解决方案是更新用户的属性时, user.account 实例重置自我。你可以在users_attributes setter方法​​做到这一点。

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

这样用户的帐户将指向这样 account.users.size 同一帐户实例应归还金额。在这种情况下,你会保持在用户模式的验证。

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.

这篇关于验证最大金额AF相关联的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆