[Feature] Add :destroy_if option to .accepts_nested_attributes_for

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Howdy, folks!

I'd like to suggest adding a stronger version of the :reject_if option to the .accepts_nested_attributes_for class method, one that not only prevents new records from being added, but also destroys existing records, using the same supplied logic.

Use-case:

A person has many email addresses, and the person wants to update their profile by adding and removing email addresses in a form. This form is designed to be a simple list of text fields, one for each email address. Users can update an existing email address by simply editing the text field, create a new email address by filling in a new text field, and remove existing email addresses by clearing out a text field. Simplicity and consistency is the goal here.

However, .accepts_nested_attributes_for's :reject_if option only permits 2/3 of these use-cases, so I have created a gem that adds a new :destroy_if option, and this permits all three use-cases! I am posting here to see if there is interest in merging this (in one form or another) into ActiveRecord itself. More details are in the README of the gem:


What do y'all think? Feedback very welcome. Thank you for your time!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Mehmet Aydoğdu
I cleaned several methods in the models using the package. It is nice to handle this feature in one line, thanks.

Micah Geisel <[hidden email]>, 24 Oca 2020 Cum, 03:41 tarihinde şunu yazdı:
Howdy, folks!

I'd like to suggest adding a stronger version of the :reject_if option to the .accepts_nested_attributes_for class method, one that not only prevents new records from being added, but also destroys existing records, using the same supplied logic.

Use-case:

A person has many email addresses, and the person wants to update their profile by adding and removing email addresses in a form. This form is designed to be a simple list of text fields, one for each email address. Users can update an existing email address by simply editing the text field, create a new email address by filling in a new text field, and remove existing email addresses by clearing out a text field. Simplicity and consistency is the goal here.

However, .accepts_nested_attributes_for's :reject_if option only permits 2/3 of these use-cases, so I have created a gem that adds a new :destroy_if option, and this permits all three use-cases! I am posting here to see if there is interest in merging this (in one form or another) into ActiveRecord itself. More details are in the README of the gem:


What do y'all think? Feedback very welcome. Thank you for your time!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com.


--

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/CA%2BMUYm%3DO-c0U%2Bh9hHOwJe_mMM7%3Dt0y1h%3DsRJVp5X_%3DKXpzCLrg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Hi Mehmet! That's great to hear! I had tried accomplishing this via lifecycle hooks, and then via setters, and found that it was really hard to cover all the edge cases, and that it felt like I was fighting with the functionality in .accepts_nested_arguments_for. Glad to hear that its not just me who has a use for this. Knowing that you're using it, I'll fill out the Travis testing matrix to test more rubies and rails, instead of just the combo my app is using.

On Friday, January 24, 2020 at 8:29:29 AM UTC-8, Mehmet Aydoğdu wrote:
I cleaned several methods in the models using the package. It is nice to handle this feature in one line, thanks.

Micah Geisel <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="wOjgPNEPEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">origino...@...>, 24 Oca 2020 Cum, 03:41 tarihinde şunu yazdı:
Howdy, folks!

I'd like to suggest adding a stronger version of the :reject_if option to the .accepts_nested_attributes_for class method, one that not only prevents new records from being added, but also destroys existing records, using the same supplied logic.

Use-case:

A person has many email addresses, and the person wants to update their profile by adding and removing email addresses in a form. This form is designed to be a simple list of text fields, one for each email address. Users can update an existing email address by simply editing the text field, create a new email address by filling in a new text field, and remove existing email addresses by clearing out a text field. Simplicity and consistency is the goal here.

However, .accepts_nested_attributes_for's :reject_if option only permits 2/3 of these use-cases, so I have created a gem that adds a new :destroy_if option, and this permits all three use-cases! I am posting here to see if there is interest in merging this (in one form or another) into ActiveRecord itself. More details are in the README of the gem:

<a href="https://github.com/botandrose/active_record-nested_attributes-destroy_if" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fbotandrose%2Factive_record-nested_attributes-destroy_if\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFsNS2DcErNDo5TLxn3-j33eRp9CA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fbotandrose%2Factive_record-nested_attributes-destroy_if\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFsNS2DcErNDo5TLxn3-j33eRp9CA&#39;;return true;">https://github.com/botandrose/active_record-nested_attributes-destroy_if 

What do y'all think? Feedback very welcome. Thank you for your time!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="wOjgPNEPEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com.


--
 
<a href="https://about.me/mehmetaydogdu?promo=email_sig&amp;utm_source=email_sig&amp;utm_medium=email_sig&amp;utm_campaign=external_links" style="margin:0px;padding:0px;border:0px;outline:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:rgb(43,130,173);text-decoration:none;display:inline-block" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fabout.me%2Fmehmetaydogdu%3Fpromo%3Demail_sig%26utm_source%3Demail_sig%26utm_medium%3Demail_sig%26utm_campaign%3Dexternal_links\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFElF5uBHxe6pD9tHHCVoCJv8E0WA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fabout.me%2Fmehmetaydogdu%3Fpromo%3Demail_sig%26utm_source%3Demail_sig%26utm_medium%3Demail_sig%26utm_campaign%3Dexternal_links\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFElF5uBHxe6pD9tHHCVoCJv8E0WA&#39;;return true;">
--
 

Mehmet Aydoğdu
https://about.me/mehmetaydogdu

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/124291e2-a7a3-433a-9464-af1b500f1215%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Okay, just released version v0.2.0, which expands support to Ruby 2.5, 2.6, and 2.7, and Rails 5.1, 5.2, and 6.0. No changes to the code needed to be made, it turns out. Probably works on older Rubies and Rails, too, but this was the low hanging fruit. PRs welcome for more support.

Rails core folks: would submitting a PR at this point be premature? I'm not clear on how this process works.

On Friday, January 24, 2020 at 9:23:11 AM UTC-8, Micah Geisel wrote:
Hi Mehmet! That's great to hear! I had tried accomplishing this via lifecycle hooks, and then via setters, and found that it was really hard to cover all the edge cases, and that it felt like I was fighting with the functionality in .accepts_nested_arguments_for. Glad to hear that its not just me who has a use for this. Knowing that you're using it, I'll fill out the Travis testing matrix to test more rubies and rails, instead of just the combo my app is using.

On Friday, January 24, 2020 at 8:29:29 AM UTC-8, Mehmet Aydoğdu wrote:
I cleaned several methods in the models using the package. It is nice to handle this feature in one line, thanks.

Micah Geisel <[hidden email]>, 24 Oca 2020 Cum, 03:41 tarihinde şunu yazdı:
Howdy, folks!

I'd like to suggest adding a stronger version of the :reject_if option to the .accepts_nested_attributes_for class method, one that not only prevents new records from being added, but also destroys existing records, using the same supplied logic.

Use-case:

A person has many email addresses, and the person wants to update their profile by adding and removing email addresses in a form. This form is designed to be a simple list of text fields, one for each email address. Users can update an existing email address by simply editing the text field, create a new email address by filling in a new text field, and remove existing email addresses by clearing out a text field. Simplicity and consistency is the goal here.

However, .accepts_nested_attributes_for's :reject_if option only permits 2/3 of these use-cases, so I have created a gem that adds a new :destroy_if option, and this permits all three use-cases! I am posting here to see if there is interest in merging this (in one form or another) into ActiveRecord itself. More details are in the README of the gem:

<a href="https://github.com/botandrose/active_record-nested_attributes-destroy_if" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fbotandrose%2Factive_record-nested_attributes-destroy_if\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFsNS2DcErNDo5TLxn3-j33eRp9CA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fbotandrose%2Factive_record-nested_attributes-destroy_if\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFsNS2DcErNDo5TLxn3-j33eRp9CA&#39;;return true;">https://github.com/botandrose/active_record-nested_attributes-destroy_if 

What do y'all think? Feedback very welcome. Thank you for your time!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com.


--
 
<a href="https://about.me/mehmetaydogdu?promo=email_sig&amp;utm_source=email_sig&amp;utm_medium=email_sig&amp;utm_campaign=external_links" style="margin:0px;padding:0px;border:0px;outline:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:rgb(43,130,173);text-decoration:none;display:inline-block" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fabout.me%2Fmehmetaydogdu%3Fpromo%3Demail_sig%26utm_source%3Demail_sig%26utm_medium%3Demail_sig%26utm_campaign%3Dexternal_links\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFElF5uBHxe6pD9tHHCVoCJv8E0WA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fabout.me%2Fmehmetaydogdu%3Fpromo%3Demail_sig%26utm_source%3Demail_sig%26utm_medium%3Demail_sig%26utm_campaign%3Dexternal_links\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFElF5uBHxe6pD9tHHCVoCJv8E0WA&#39;;return true;">
--
 

Mehmet Aydoğdu
https://about.me/mehmetaydogdu

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/437183f1-8817-4fab-94da-2ea15b12bd33%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Pedro Fernandes Steimbruch
Doesn't :allow_destroy help on you use cases?

On Mon, Jan 27, 2020 at 2:16 PM Micah Geisel <[hidden email]> wrote:
Okay, just released version v0.2.0, which expands support to Ruby 2.5, 2.6, and 2.7, and Rails 5.1, 5.2, and 6.0. No changes to the code needed to be made, it turns out. Probably works on older Rubies and Rails, too, but this was the low hanging fruit. PRs welcome for more support.

Rails core folks: would submitting a PR at this point be premature? I'm not clear on how this process works.

On Friday, January 24, 2020 at 9:23:11 AM UTC-8, Micah Geisel wrote:
Hi Mehmet! That's great to hear! I had tried accomplishing this via lifecycle hooks, and then via setters, and found that it was really hard to cover all the edge cases, and that it felt like I was fighting with the functionality in .accepts_nested_arguments_for. Glad to hear that its not just me who has a use for this. Knowing that you're using it, I'll fill out the Travis testing matrix to test more rubies and rails, instead of just the combo my app is using.

On Friday, January 24, 2020 at 8:29:29 AM UTC-8, Mehmet Aydoğdu wrote:
I cleaned several methods in the models using the package. It is nice to handle this feature in one line, thanks.

Micah Geisel <[hidden email]>, 24 Oca 2020 Cum, 03:41 tarihinde şunu yazdı:
Howdy, folks!

I'd like to suggest adding a stronger version of the :reject_if option to the .accepts_nested_attributes_for class method, one that not only prevents new records from being added, but also destroys existing records, using the same supplied logic.

Use-case:

A person has many email addresses, and the person wants to update their profile by adding and removing email addresses in a form. This form is designed to be a simple list of text fields, one for each email address. Users can update an existing email address by simply editing the text field, create a new email address by filling in a new text field, and remove existing email addresses by clearing out a text field. Simplicity and consistency is the goal here.

However, .accepts_nested_attributes_for's :reject_if option only permits 2/3 of these use-cases, so I have created a gem that adds a new :destroy_if option, and this permits all three use-cases! I am posting here to see if there is interest in merging this (in one form or another) into ActiveRecord itself. More details are in the README of the gem:


What do y'all think? Feedback very welcome. Thank you for your time!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/6fa3f3a2-5637-4471-9eae-ae667907e255%40googlegroups.com.


--

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/437183f1-8817-4fab-94da-2ea15b12bd33%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/CAA2Dq%2BPUsium%2Bjy8vDnTPBQYJWC1PdqEDSLhyQQuja0CuYy85A%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Hello, Pedro! Thank you for the message.

My understanding is that :allow_destroy simply enables the _destroy param. Is this not the case, and I'm reinventing existing functionality?

https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Rereading the documentation, it appears as though my understanding is correct. If it's not, though, that would be great. I'd love to already have this functionality!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Pedro Fernandes Steimbruch
Hey,

you're right in your understanding. :allow_destroy will enable the _destroy param and in your use case, can't you send the _destroy in the request?

On Mon, Jan 27, 2020 at 10:30 PM Micah Geisel <[hidden email]> wrote:
Hello, Pedro! Thank you for the message.

My understanding is that :allow_destroy simply enables the _destroy param. Is this not the case, and I'm reinventing existing functionality?

https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Rereading the documentation, it appears as though my understanding is correct. If it's not, though, that would be great. I'd love to already have this functionality!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/CAA2Dq%2BMDUDCj9F7RYNqLd-O3oSETQ0HRPSCCYLhkDpYsK3GbBg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Yes, I could write some JavaScript to inspect the text fields on their change event, and then toggle a hidden _destroy param to cover the third use-case, but this has some significant downsides, compared to :destroy_if solution that I'm proposing here.

1. Decreased locality. Logic to handle nested params would be both in the model, and also in the client in JavaScript
2. Increased complexity. The JavaScript component is more lines of code, and thus more things that can go wrong. What if the client is running NoScript, etc?
3. Code duplication. The predicate logic that is passed to :ignore_if would have to also be written in JavaScript.

Compared to this, :destroy_if appears to be a much simpler and cleaner solution, and has none of these downsides.
.
On Tuesday, January 28, 2020 at 2:03:36 AM UTC-8, Pedro Fernandes Steimbruch wrote:
Hey,

you're right in your understanding. :allow_destroy will enable the _destroy param and in your use case, can't you send the _destroy in the request?

On Mon, Jan 27, 2020 at 10:30 PM Micah Geisel <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="YuwHtBQ1EwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">origino...@...> wrote:
Hello, Pedro! Thank you for the message.

My understanding is that :allow_destroy simply enables the _destroy param. Is this not the case, and I'm reinventing existing functionality?

<a href="https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FNestedAttributes%2FClassMethods.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGcw_qE-NC8yGllLJFiz6bIIh6chw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FNestedAttributes%2FClassMethods.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGcw_qE-NC8yGllLJFiz6bIIh6chw&#39;;return true;">https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Rereading the documentation, it appears as though my understanding is correct. If it's not, though, that would be great. I'd love to already have this functionality!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YuwHtBQ1EwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/f0771ade-97ab-4bc5-a89d-4ecae4174990%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] Add :destroy_if option to .accepts_nested_attributes_for

Micah Geisel
Sorry, typo in 3. Should be :reject_if, not :ignore_if.

On Tuesday, January 28, 2020 at 7:07:47 AM UTC-8, Micah Geisel wrote:
Yes, I could write some JavaScript to inspect the text fields on their change event, and then toggle a hidden _destroy param to cover the third use-case, but this has some significant downsides, compared to :destroy_if solution that I'm proposing here.

1. Decreased locality. Logic to handle nested params would be both in the model, and also in the client in JavaScript
2. Increased complexity. The JavaScript component is more lines of code, and thus more things that can go wrong. What if the client is running NoScript, etc?
3. Code duplication. The predicate logic that is passed to :ignore_if would have to also be written in JavaScript.

Compared to this, :destroy_if appears to be a much simpler and cleaner solution, and has none of these downsides.
.
On Tuesday, January 28, 2020 at 2:03:36 AM UTC-8, Pedro Fernandes Steimbruch wrote:
Hey,

you're right in your understanding. :allow_destroy will enable the _destroy param and in your use case, can't you send the _destroy in the request?

On Mon, Jan 27, 2020 at 10:30 PM Micah Geisel <[hidden email]> wrote:
Hello, Pedro! Thank you for the message.

My understanding is that :allow_destroy simply enables the _destroy param. Is this not the case, and I'm reinventing existing functionality?

<a href="https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FNestedAttributes%2FClassMethods.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGcw_qE-NC8yGllLJFiz6bIIh6chw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FNestedAttributes%2FClassMethods.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGcw_qE-NC8yGllLJFiz6bIIh6chw&#39;;return true;">https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Rereading the documentation, it appears as though my understanding is correct. If it's not, though, that would be great. I'd love to already have this functionality!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-core/5580a000-b636-4781-9649-7fedbb80432f%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/df938809-678b-4696-9978-3944df47dc24%40googlegroups.com.