has_many association limit in Rails

Author: Stuart Maynes

Imagine you have an Organisation and User model and each Organisation has many Users. However, each Organisation has a maximum number of Users.
Using only nice idiomatic Rails, do you enforce this constraint at the model level?
There’s lots of nasty ways to do this that I’ve bodged together in the past, but yesterday I came across association callbacks which completely solves this issue. See example code below.