This project is archived and is in readonly mode.
[PATCH] ActiveRecord::AutosaveAssociation#unmark_for_destruction
Reported by Levin Alexander | December 8th, 2009 @ 04:49 PM
It should be possible to decide to keep an element that has already been marked for destruction (My use case is that I want do delete everything in an association except a few select elements)
The patch adds a new method unmark_for_destruction
that clears this flag.
I'm not entirely sure about the name. Maybe
mark_for_destruction(false)
or
mark_for_destruction = false
would be better?
(The Patch is against 2-3-stable but should also cleanly apply on master)
Comments and changes to this ticket
-
Levin Alexander December 8th, 2009 @ 05:00 PM
- Tag changed from 2.3.x, activerecord, autosave_association, marked_for_destruction to 2.3.x, activerecord, autosave_association, marked_for_destruction, patch
- Title changed from ActiveRecord::AutosaveAssociation#unmark_for_destruction to [PATCH] ActiveRecord::AutosaveAssociation#unmark_for_destruction
-
Eloy Duran December 28th, 2009 @ 05:16 PM
Could you give me your use case? I'm missing the context which I need to decide how to handle this. Right now I'm not sure why you would allow the user to marks something for destruction, but in the end remove the mark…
-
Eloy Duran December 28th, 2009 @ 05:16 PM
- State changed from new to incomplete
-
Levin Alexander December 28th, 2009 @ 08:21 PM
I have a widget with many parts. I am using (something like) nested attributes, but don't want to have to send
_delete
-attributes when a part is deleted. Instead I want to send a new list of updated parts and have the application figure out the rest.As an example, this Widget:
Widget: Part: id: 1, color: green, name: dial Part: id: 2, color: red, name: switch Part: id: 3, color: blue, name: knob
I am updating this Widget, sending a new list of parts (parts that are not sent should be destroyed):
:part_attributes => [ { :id => 2, :color => :red, :name => "switch"}, { :id => 3, :color => :blue, :name => "knob" } ], ...
This should delete the "dial" part, but keep "switch" and "knob" (and not change their IDs)
The simplest way to implement this (with the patch) is to mark every part as deleted and then selectively keep everything that is still used.
The alternative (without the patch) is to keep a list of items that are still used, and then delete everything.
so with the patch:
def update_widget_parts parts.each do |part| part.mark_for_destruction @part_attributes.each do |part_attribute| part = parts.find_and_update_or_build_from_attributes(part_attribute) part.unmark_for_destruction end end
without the patch:
def update_widget_parts parts_to_keep = [] @part_attributes.each do |part_attribute| part = parts.find_and_update_or_build_from_attributes(part_attribute) parts_to_keep << part.id unless part.new_record? end parts.each do |part| part.mark_for_destruction unless part.new_record? or parts_to_keep.include?(part.id) end end
-
Levin Alexander December 28th, 2009 @ 09:05 PM
Joshua White proposed
:destroy_missing
in Ticket #2563 which addresses this use case. (I'd still like this Patch to be applied because I'm not using nested_attributes) -
Levin Alexander December 29th, 2009 @ 05:02 AM
Short version of the above:
I want to implement :destroy_missing, and
mark_for_destruction = false
/unmark_for_destruction
would give me a nice way to do this. -
Eloy Duran December 30th, 2009 @ 03:44 PM
- State changed from incomplete to open
I understand. How about providing a way to do this with nested attributes and without it? (See attached patch)
You could then use AssociationCollection#mark_missing_records_for_destruction to mark the missing records for destruction yourself.
-
Eloy Duran January 8th, 2010 @ 10:58 AM
- State changed from open to hold
The current patch for destroy_missing can be found on this branch: http://github.com/Fingertips/rails/commits/2-3-experimental
Any update on whether or not you can use this?
-
Jens April 16th, 2010 @ 06:13 AM
unmark_for_destruction would also come in handy if you want to prevent destruction in certain cases. I was about to open another bug but this might just be another use case for this feature:
I need a way to prevent destruction of child objects connected to a parent object when certain conditions are met.
For nested records, I want to provide a form to the user with :delete / :destroy checkboxes. In some cases the user should not be able to delete a child object, there I omit the checkbox, BUT this also needs to be checked server side, because the user can theoretically modify the HTML code and re-enable the checkbox.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>