1

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.

Martin
  • 11,216
  • 23
  • 83
  • 140

1 Answers1

3

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
agmcleod
  • 13,321
  • 13
  • 57
  • 96