Skip to content

Instantly share code, notes, and snippets.

@jesskturner
Created April 14, 2016 14:01
Show Gist options
  • Save jesskturner/47f6b611bf39aa46164b7f5799a902a2 to your computer and use it in GitHub Desktop.
Save jesskturner/47f6b611bf39aa46164b7f5799a902a2 to your computer and use it in GitHub Desktop.
Limit has_many associations to a set limit
class User
has_many :things
end
class Thing
belongs_to :user
validate :on => :create do
if user && user.things.length >= thing_limit
errors.add(:user, :too_many_things)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment