Skip to content
Permalink
Browse files
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
durran authored and spastorino committed Apr 5, 2011
1 parent 284ca81 commit a9b4b5da7c216e4464eeb9dbd0a39ea258d64325
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
@@ -452,6 +452,7 @@ def reject_new_record?(association_name, attributes)
end

def call_reject_if(association_name, attributes)
return false if has_destroy_flag?(attributes)
case callback = self.nested_attributes_options[association_name][:reject_if]
when Symbol
method(callback).arity == 0 ? send(callback) : send(callback, attributes)
@@ -131,6 +131,14 @@ def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
assert_equal 'photography', interest.reload.topic
end

def test_destroy_works_independent_of_reject_if
Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true
man = Man.create(:name => "Jon")
interest = man.interests.create(:topic => 'the ladies')
man.update_attributes({:interests_attributes => { :_destroy => "1", :id => interest.id } })
assert man.reload.interests.empty?
end

def test_has_many_association_updating_a_single_record
Man.accepts_nested_attributes_for(:interests)
man = Man.create(:name => 'John')

0 comments on commit a9b4b5d

Please sign in to comment.