Created
April 14, 2016 14:01
-
-
Save jesskturner/47f6b611bf39aa46164b7f5799a902a2 to your computer and use it in GitHub Desktop.
Limit has_many associations to a set limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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