views:

18

answers:

1

I know Rails has a one_to_many relationship, but I want to enforce that "many" is at least one object. What's the most elegant way to do this?

A: 

Add a validation in your one model to check there are most of one many object


def validates
  errors.add(:many_objects, 'You need at least one object') if many_objects.empty?
end
shingara