Validating relationship exists

Good evening,

Trying to find a good way to validate that a relationship exists. For
instance, let’s say I have a Movie and every move HAS TO HAVE a director
(so, movie belongs_to director); I’ve enforced the constraint in my
schema but I’d like to take the abstraction up to the application.

I’ve seen the validates_associated bits but I’m not sure if that’s
adequate. Perhaps validates_presence_of?

Any help would be most welcome!

On Jan 29, 11:49 am, Cory W. [email protected]

Any help would be most welcome!

Posted via

Validates_associated will check the referenced object exists (ie a
director with that id is in the Directors table).
Validated_presence_of will make sure a value has been specified when
you are creating a movie (you can’t pass a null/nil value when
creating a new movie).
Between the two you should have what you need, but to be sure you
could implement a before_create filter in the Movie model if you need
to do fancy stuff.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs