I have a model User which has one to many relation to the Image model.
How can I limit user to be able to store only 3 images?
I have a model User which has one to many relation to the Image model.
How can I limit user to be able to store only 3 images?
How about validation?
class Image
belongs_to :user
validate :max_3_per_user
# (...)
private
def max_3_per_user
if user_id && Image.where(user_id: user_id).count >= 3
errors.add(:images, 'There can only be 3 per user')
end
end
end