validates_associated doesn’t

As of Rails 2.1.1, validates_associated still does not actually do anything useful. See bug 5369. You would expect that it would validate that the foreign_key refers to a valid row, but no.

Ryan Bates points out the alternative DIY solution:

validate :book_must_exist
 
def book_must_exist
  errors.add(:book_id, "must point to an existing book") if book_id && book.nil?
end

You must be logged in to post a comment.