diff options
author | Jolyon Pawlyn <jpawlyn@gmail.com> | 2018-05-01 19:05:52 +0200 |
---|---|---|
committer | Jolyon Pawlyn <jpawlyn@gmail.com> | 2018-05-01 19:05:52 +0200 |
commit | 66280b9eb9a3263971377f80f23ef29428c3b974 (patch) | |
tree | e004d3af9cef3cedcab5685f57518633f69b211f /activerecord/lib | |
parent | 24aeccb1c873cb8bbf00ff77cb33432cfe598b3d (diff) | |
download | rails-66280b9eb9a3263971377f80f23ef29428c3b974.tar.gz rails-66280b9eb9a3263971377f80f23ef29428c3b974.tar.bz2 rails-66280b9eb9a3263971377f80f23ef29428c3b974.zip |
Allow a belonging to object to be created from a new record
If a 'has one' object is created from a new record, an ActiveRecord::RecordNotSaved error is raised but this behavior was also applied to the reverse scenario.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 4 |
2 files changed, 8 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 090b082cb0..4e8e7ee43b 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -107,6 +107,14 @@ module ActiveRecord yield end end + + def _create_record(attributes, raise_error = false) + unless owner.persisted? + raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" + end + + super + end end end end diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 441bd715e4..ead89bfe6c 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -63,10 +63,6 @@ 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 |