has_many relationship - specify how quantity (how many)


Is there any way to specify a a validation rule or something for the
"has_many' relationship so that it would count the related objects?

For e.g. I want my Foo object to have maximum of 5 Bar objects in
has_many relationship.


I suppose you could write a custom validator to ensure that no more than 5 child objects exist.

validates :bar_limit

def bar_limit