Skip to content
This repository
Browse code

Nested attribute accessors should ignore new records with truthy _del…

…ete key.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1861 state:committed]
  • Loading branch information...
commit 455a7633dbdb295de828eb2657433d47d85eb0bc 1 parent 4e4f961
Pascal Ehlert pehlert authored NZKoz committed
8 activerecord/lib/active_record/nested_attributes.rb
@@ -86,7 +86,8 @@ def self.included(base)
86 86 # For each key in the hash that starts with the string 'new' a new model
87 87 # will be instantiated. When the proc given with the <tt>:reject_if</tt>
88 88 # option evaluates to +false+ for a certain attribute hash no record will
89   - # be built for that hash.
  89 + # be built for that hash. (Rejecting new records can alternatively be done
  90 + # by utilizing the <tt>'_delete'</tt> key. Scroll down for more info.)
90 91 #
91 92 # params = { 'member' => {
92 93 # 'name' => 'joe', 'posts_attributes' => {
@@ -258,11 +259,14 @@ def should_destroy_nested_attributes_record?(allow_destroy, attributes)
258 259 # If a <tt>:reject_if</tt> proc exists for this association, it will be
259 260 # called with the attributes as its argument. If the proc returns a truthy
260 261 # value, the record is _not_ build.
  262 + #
  263 + # Alternatively, you can specify the <tt>'_delete'</tt> key to _not_ build
  264 + # a record. See should_destroy_nested_attributes_record? for more info.
261 265 def build_new_nested_attributes_record(association_name, attributes)
262 266 if reject_proc = self.class.reject_new_nested_attributes_procs[association_name]
263 267 return if reject_proc.call(attributes)
264 268 end
265   - send(association_name).build(attributes)
  269 + send(association_name).build(attributes) unless should_destroy_nested_attributes_record?(true, attributes)
266 270 end
267 271
268 272 # Assigns the attributes to the record specified by +id+. Or marks it for
14 activerecord/test/cases/nested_attributes_test.rb
@@ -233,6 +233,20 @@ def test_should_automatically_build_new_associated_models_for_each_entry_in_a_ha
233 233 assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
234 234 end
235 235
  236 + def test_should_remove_delete_key_from_arguments_hash_of_new_records
  237 + assert_nothing_raised ActiveRecord::UnknownAttributeError do
  238 + @pirate.send(association_setter, { 'new_1' => { '_delete' => '0' }})
  239 + end
  240 + end
  241 +
  242 + def test_should_ignore_new_associated_records_with_truthy_delete_attribute
  243 + @pirate.send(@association_name).destroy_all
  244 + @pirate.reload.attributes = { association_getter => { 'new_1' => { :name => 'Grace OMalley' }, 'new_2' => { :name => 'Privateers Greed', '_delete' => '1' }}}
  245 +
  246 + assert_equal 1, @pirate.send(@association_name).length
  247 + assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
  248 + end
  249 +
236 250 def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models
237 251 attributes = ActiveSupport::OrderedHash.new
238 252 attributes['new_123726353'] = { :name => 'Grace OMalley' }

0 comments on commit 455a763

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