forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
belongs_to
's optional
option to handle data integ…
…rity. By adding this support, model can able to save foreign_key to either `nil` value or to the valid foreign_key value which means foreign_key value must present in the foreign table. class Post < ActiveRecord::Base belongs_to :author, optional: :with_integrity end author = Author.create! post = Post.create!(author_id: author.id) post.author_id = 0 # Invalid id which is not present in database post.save! # Should raise 'ActiveRecord::RecordInvalid' error post.author_id = nil post.save! # No Error Based on this issue's discussion rails#26557. Added this.
- Loading branch information
1 parent
8150c12
commit e13f6cf
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters