Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个与模型User具有一对多关系的Image模型。
User
Image
如何限制用户只能存储 3 张图像?
验证呢?
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