Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: why isn't this marked_for_destruction not destroyed?
    text
    copied!<p>** update ** it all seems to be related to a custom validator: if I remove it, it works as expected. see code at the end **</p> <p>I have a model <code>budget</code> that has many <code>multi_year_impacts</code></p> <p>in the console, if I run:</p> <pre><code>b = Budget.find(4) b.multi_year_impacts.size #=&gt; 2 b.update_attributes({multi_year_impacts_attributes: {id: 20, _destroy: true} } ) #=&gt; true b.multi_year_impacts.size #=&gt; 1 (so far so good) b.reload b.multi_year_impacts.size #=&gt; 2 What??? </code></pre> <p>and if before <code>b.reload</code> I do <code>b.save</code> (which shouldn't be needed anyway), it's the same.</p> <p><strong>Any idea why my child record doesn't get destroyed?</strong></p> <p>Some additional information, just in case:</p> <p>Rails 3.2.12</p> <p>in <code>budget.rb</code></p> <pre><code>attr_accessible :multi_year_impacts_attributes has_many :multi_year_impacts, as: :impactable, :dependent =&gt; :destroy accepts_nested_attributes_for :multi_year_impacts, :allow_destroy =&gt; true validates_with MultiYearImpactValidator # problem seems to com from here </code></pre> <p>in <code>multi_year_impact.rb</code></p> <pre><code>belongs_to :impactable, polymorphic: true </code></pre> <p>in <code>multi_year_impact_validator.rb</code></p> <pre><code>class MultiYearImpactValidator &lt; ActiveModel::Validator def validate(record) return false unless record.amount_before &amp;&amp; record.amount_after &amp;&amp; record.savings lines = record.multi_year_impacts.delete_if{|x| x.marked_for_destruction?} %w[amount_before amount_after savings].each do |val| if lines.inject(0){|s,e| s + e.send(val).to_f} != record.send(val) record.errors.add(val.to_sym, " please check \"Repartition per year\" below: the sum of all lines must be equal of total amounts") end end end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload