aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-06-17 10:08:32 +0930
committerGitHub <noreply@github.com>2017-06-17 10:08:32 +0930
commitf8f792fa4f7d393f1c00aaef44a76917ddb6c2fb (patch)
tree9ac6ba56a01c9fdf83dcc810669273ebd44662b3 /activerecord/lib/active_record
parenta1c8b4612373527333e59baca6d5fcf5598dd6eb (diff)
parent9048a70f34b8109be24b2da1009a2f4607ec66bf (diff)
downloadrails-f8f792fa4f7d393f1c00aaef44a76917ddb6c2fb.tar.gz
rails-f8f792fa4f7d393f1c00aaef44a76917ddb6c2fb.tar.bz2
rails-f8f792fa4f7d393f1c00aaef44a76917ddb6c2fb.zip
Merge pull request #29392 from alexcameron89/unpersisted_create_association
Raise on create for singular association when parent is unpersisted
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb4
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