Skip to content

Commit a9b4b5d

Browse files
durranspastorino
authored andcommitted
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>
1 parent 284ca81 commit a9b4b5d

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

activerecord/lib/active_record/nested_attributes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def reject_new_record?(association_name, attributes)
452452
end
453453

454454
def call_reject_if(association_name, attributes)
455+
return false if has_destroy_flag?(attributes)
455456
case callback = self.nested_attributes_options[association_name][:reject_if]
456457
when Symbol
457458
method(callback).arity == 0 ? send(callback) : send(callback, attributes)

activerecord/test/cases/nested_attributes_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
131131
assert_equal 'photography', interest.reload.topic
132132
end
133133

134+
def test_destroy_works_independent_of_reject_if
135+
Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true
136+
man = Man.create(:name => "Jon")
137+
interest = man.interests.create(:topic => 'the ladies')
138+
man.update_attributes({:interests_attributes => { :_destroy => "1", :id => interest.id } })
139+
assert man.reload.interests.empty?
140+
end
141+
134142
def test_has_many_association_updating_a_single_record
135143
Man.accepts_nested_attributes_for(:interests)
136144
man = Man.create(:name => 'John')

0 commit comments

Comments
 (0)