Forum: Ruby on Rails validating relationship exists

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ee7be48e775d357de69db2754a377160?d=identicon&s=25 Cory Wilkerson (Guest)
on 2007-01-29 01:49
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!
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2007-01-29 02:22
(Received via mailing list)
On Jan 29, 11:49 am, Cory Wilkerson <>
> 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 topic is locked and can not be replied to.