How do I access ActiveRecord :: AutosaveAssociation.marked_for_destruction? from within the parent model

I have a Person model, with the following association:

has_many :distributions

accepts_nested_attributes_for :distributions, :allow_destroy => true

validate :distributions_must_total_100


Custom checkout currently fails when it shouldn't - when some checks were marked for destruction - because they still show up in the "distributions" attributes (in memory, never mind what is in the db) before Face and its distributions are saved.

I would like to use ActiveRecord::AutosaveAssociation.marked_for_destruction?

in my validator to ignore distributions that need to be destroyed.

  • How do I achieve this method? Not really sure if the distribution relation (your normal Rails model) applies to this class.

  • Is this an awful unclean hack? It feels a little messy and this is the most accurate solution to the problem.

I went through the ActiveRecord source code in the debugger to confirm that the records are actually marked for destruction (i.e. not a Javascript or form error).


source to share

1 answer

It turns out you can just call marked_for_destruction?

on the Distribution instance and it works great.



All Articles