Forum: Ruby on Rails checking foreign keys on validate

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-11-16 02:33
(Received via mailing list)
Hi,

I've noticed that by default AR#validate doesn't check whether a given
foreign key relationship is actually valid. So if you have a book and
you set its author_id to some number that doesn't correspond to an
Author in the authors table, then it valid? will just return true. I
guess this is a rare condition because normally the author_id would come
from a pre-populated, but I just ran into this issue in one of my unit
tests.

So I guess my question is: is their an easy way to check if the
associations exist before inserting/updating a record?

Jeroen
This topic is locked and can not be replied to.