Skip to content
This repository
Browse code

Destroying records via nested attributes works independent of reject_if:

- When a :_destroy truthiness is provided in the attributes hash, the
  record should get destroyed regardless of the result of the proc or
    method supplied to :reject_if. (If :allow_destroy is true)

[#6006 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
commit 30264bb57b0f54c5bdd7cd23d0a29b36879954a3 1 parent 5b8dbb0
Durran Jordan authored April 05, 2011 spastorino committed April 05, 2011
1  activerecord/lib/active_record/nested_attributes.rb
@@ -402,6 +402,7 @@ def reject_new_record?(association_name, attributes)
402 402
     end
403 403
 
404 404
     def call_reject_if(association_name, attributes)
  405
+      return false if has_destroy_flag?(attributes)
405 406
       case callback = nested_attributes_options[association_name][:reject_if]
406 407
       when Symbol
407 408
         method(callback).arity == 0 ? send(callback) : send(callback, attributes)
16  activerecord/test/cases/nested_attributes_test.rb
@@ -139,6 +139,22 @@ def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
139 139
     assert_equal 'photography', interest.reload.topic
140 140
   end
141 141
 
  142
+  def test_destroy_works_independent_of_reject_if
  143
+    Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true
  144
+    man = Man.create(:name => "Jon")
  145
+    interest = man.interests.create(:topic => 'the ladies')
  146
+    man.update_attributes({:interests_attributes => { :_destroy => "1", :id => interest.id } })
  147
+    assert man.reload.interests.empty?
  148
+  end
  149
+
  150
+  def test_has_many_association_updating_a_single_record
  151
+    Man.accepts_nested_attributes_for(:interests)
  152
+    man = Man.create(:name => 'John')
  153
+    interest = man.interests.create(:topic => 'photography')
  154
+    man.update_attributes({:interests_attributes => {:topic => 'gardening', :id => interest.id}})
  155
+    assert_equal 'gardening', interest.reload.topic
  156
+  end
  157
+
142 158
   def test_reject_if_with_blank_nested_attributes_id
143 159
     # When using a select list to choose an existing 'ship' id, with :include_blank => true
144 160
     Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| attributes[:id].blank? }

0 notes on commit 30264bb

Please sign in to comment.
Something went wrong with that request. Please try again.