diff options
author | Alex Kitchens <alexcameron98@gmail.com> | 2017-06-08 16:11:45 -0500 |
---|---|---|
committer | Alex Kitchens <alexcameron98@gmail.com> | 2017-06-08 16:32:10 -0500 |
commit | 9048a70f34b8109be24b2da1009a2f4607ec66bf (patch) | |
tree | 94e47a04f2a311be048adbdce298dc9e0fe74f09 /activerecord/lib/active_record | |
parent | 73715f29ab4c56ebcd74403903ebd691677ef318 (diff) | |
download | rails-9048a70f34b8109be24b2da1009a2f4607ec66bf.tar.gz rails-9048a70f34b8109be24b2da1009a2f4607ec66bf.tar.bz2 rails-9048a70f34b8109be24b2da1009a2f4607ec66bf.zip |
Raise on create for singular association when parent is unpersisted
A collection association will raise on `#create_association` when the parent is
unpersisted. A singular association should do the same. This addresses
issue #29219.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 91580a28d0..ca138635d2 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -63,6 +63,10 @@ module ActiveRecord end def _create_record(attributes, raise_error = false) + unless owner.persisted? + raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" + end + record = build_record(attributes) yield(record) if block_given? saved = record.save |