diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-01-29 14:46:27 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-01-29 15:06:51 +0900 |
commit | e09da8bf7853f8f4f1ebb5c52f9b688a32b4dfaa (patch) | |
tree | 0da8ff4943aa9d72e3d9a90b3ea9a10ba7be1688 /activerecord/lib | |
parent | 6a1c0218df1fcffaac97e7288db07934bfef277f (diff) | |
download | rails-e09da8bf7853f8f4f1ebb5c52f9b688a32b4dfaa.tar.gz rails-e09da8bf7853f8f4f1ebb5c52f9b688a32b4dfaa.tar.bz2 rails-e09da8bf7853f8f4f1ebb5c52f9b688a32b4dfaa.zip |
Reload `through_record` that has been destroyed in `create_through_record`
This is an alternative of #27714.
If `has_one :through` association has set `nil`, `through_record` is
destroyed but still remain loaded target in `through_proxy` until
`reload` or `reset` explicitly.
If `through_proxy` is not reset (remain destroyed (frozen) target),
setting new record causes `RuntimeError: Can't modify frozen hash`.
To prevent `RuntimeError`, should reload `through_record` that has been
destroyed in `create_through_record`.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_through_association.rb | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 604904abcc..1183bdf6f4 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -22,6 +22,10 @@ module ActiveRecord elsif record attributes = construct_join_attributes(record) + if through_record && through_record.destroyed? + through_record = through_proxy.tap(&:reload).target + end + if through_record through_record.update(attributes) elsif owner.new_record? |