diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-05-02 04:50:20 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-02 04:50:20 +0900 |
commit | 1848610d1847d21e7f18b440b23c9a6192e0133c (patch) | |
tree | 79210ed864390bd9405abe62873aaad21bf317aa /activerecord/lib | |
parent | e24eeef3d42eac4f6fdd9a63bcde2a054e839af9 (diff) | |
parent | 66280b9eb9a3263971377f80f23ef29428c3b974 (diff) | |
download | rails-1848610d1847d21e7f18b440b23c9a6192e0133c.tar.gz rails-1848610d1847d21e7f18b440b23c9a6192e0133c.tar.bz2 rails-1848610d1847d21e7f18b440b23c9a6192e0133c.zip |
Merge pull request #32784 from jpawlyn/allow-belongs-to-create-for-new-record
Allow a belonging to object to be created from a new record
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 |