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.
我有很多属于一个赞的Movie照片Movie.photos
Movie
Movie.photos
知道如何验证电影应该至少有一张照片吗?
validates_presence_of :photos不起作用或至少被认为nil是有效的。
validates_presence_of :photos
nil
我有兴趣针对真正的嵌套对象进行验证。
我不认为有一个内置的验证器,比如presence_of,所以你可以自己写。以下内容位于您的 movie.rb 文件中。
validate :at_least_one_photo private def at_least_one_photo if photos.size < 1 errors.add :base, "The movie must have at least one photo attached before saving" end end