8000 run update_resource inside a transaction to avoid autosaving relation… · activeadmin/activeadmin@c4bc6c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4bc6c4

Browse files
arbesulodeivid-rodriguez
authored andcommitted
run update_resource inside a transaction to avoid autosaving relationships through assign_attributes when the record is invalid
1 parent 2bab2ff commit c4bc6c4

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Fix autosaving relationships when submitting invalid parameters. [#7437] by [@arbesulo]
6+
57
## 2.11.1 [](https://github.com/activeadmin/activeadmin/compare/v2.11.0..v2.11.1)
68

79
### Enhancements
@@ -743,6 +745,7 @@ Please check [0-6-stable] for previous changes.
743745
[#7350]: https://github.com/activeadmin/activeadmin/pull/7350
744746
[#7384]: https://github.com/activeadmin/activeadmin/pull/7384
745747
[#7394]: https://github.com/activeadmin/activeadmin/pull/7394
748+
[#7437]: https://github.com/activeadmin/activeadmin/pull/7437
746749

747750
[@1000ship]: https://github.com/1000ship
748751
[@5t111111]: https://github.com/5t111111
@@ -755,6 +758,7 @@ Please check [0-6-stable] for previous changes.
755758
[@amiel]: https://github.com/amiel
756759
[@amiuhle]: https://github.com/amiuhle
757760
[@andreslemik]: https://github.com/andreslemik
761+
[@arbesulo]: https://github.com/arbesulo
758762
[@bartoszkopinski]: https://github.com/bartoszkopinski
759763
[@blocknotes]: https://github.com/blocknotes
760764
[@bolshakov]: https://github.com/bolshakov

lib/active_admin/resource_controller/data_access.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,16 @@ def save_resource(object)
168168
#
169169
# @return [void]
170170
def update_resource(object, attributes)
171-
object = assign_attributes(object, attributes)
172-
173-
run_update_callbacks object do
174-
save_resource(object)
171+
status = nil
172+
ActiveRecord::Base.transaction do
173+
object = assign_attributes(object, attributes)
174+
175+
run_update_callbacks object do
176+
status = save_resource(object)
177+
raise ActiveRecord::Rollback unless status
178+
end
175179
end
180+
status
176181
end
177182

178183
# Destroys an object from the database and calls appropriate callbacks.

0 commit comments

Comments
 (0)
0