From 9048a70f34b8109be24b2da1009a2f4607ec66bf Mon Sep 17 00:00:00 2001 From: Alex Kitchens Date: Thu, 8 Jun 2017 16:11:45 -0500 Subject: 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. --- activerecord/lib/active_record/associations/singular_association.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/lib/active_record') 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 -- cgit v1.2.3