code_junkie

Best practice for limiting the number of associations within a has_many relationship?

Nov 12th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.30 KB | None | 0 0
  1. belongs_to :account
  2.  
  3. has_many :users
  4.  
  5. @account.users << someuser
  6.  
  7. @account.users.create
  8.  
  9. class User
  10. belongs_to :account
  11. end
  12.  
  13. class User
  14. validates_each :account do |user, attr, value|
  15. user.errors.add attr, "too much users for account" if user.account.users.size >= 3
  16. end
  17. end
Add Comment
Please, Sign In to add comment