Reputation: 11336
I have many photos that belong to a Movie
like Movie.photos
Any idea how can I validate that a Movie should have at least one photo?
validates_presence_of :photos
doesn't work or at least is considering nil
as valid.
I'm interested in validate against a real nested object.
Upvotes: 1
Views: 646
Reputation: 13621
I don't think there's a built in validator for this, like presence_of, so you can just write your own. The following goes in your movie.rb file.
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
Upvotes: 3