Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

accepts_nested_attributes_for '_delete' feature broken in Rails 2.3.8

#4707

I created Rails 2.3.8 test application:

class User < ActiveRecord::Base
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end

class Address < ActiveRecord::Base
  belongs_to :user
end

Then I try to delete some nested object:

ruby script/console 
Loading development environment (Rails 2.3.8)
>> u = User.create(:name => "Max")
=> #<User id: 1, name: "Max", created_at: "2010-05-26 14:16:12", updated_at: "2010-05-26 14:16:12">
>> u.update_attributes(:addresses_attributes => {'0' => {:city => 'York'}})
=> true
>> u.update_attributes(:addresses_attributes => {'0' => {:id => '1', :city => 'York'}})
=> true
>> u.addresses
=> [#<Address id: 1, country: nil, city: "York", street: nil, user_id: 1, created_at: "2010-05-26 14:16:46", updated_at: "2010-05-26 14:16:46">]
>> u.update_attributes(:addresses_attributes => {'0' => {:id => '1', :city => 'York', '_delete' => '1'}})
ActiveRecord::UnknownAttributeError: unknown attribute: _delete
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `assign_attributes'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:374:in `assign_to_or_mark_for_destruction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:361:in `assign_nested_attributes_for_collection_association'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:352:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:352:in `assign_nested_attributes_for_collection_association'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:244:in `addresses_attributes='
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `assign_attributes'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2663:in `update_attributes'
    from (irb):7

When I change the Rails version to 2.3.5:

ruby script/console 
Loading development environment (Rails 2.3.5)
>> User.first.update_attributes(:addresses_attributes => {'0' => {:id => '1', :city => 'York', '_delete' => '1'}})
=> true
>> User.first.addresses
=> []

Reported by Gacha · May 26th, 2010 @ 03:29 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Phil C
    Phil C

    There is a deprecation warning in 2.3.5. Use destroy instead of delete in 2.3.8

    May 26th, 2010 @ 11:37 PM

  2. Phil C
    Phil C

    eg:

    User.first.update_attributes(:addresses_attributes => {'0' => {:id => '1', :city => 'York', '_destroy' => '1'}})
    

    May 26th, 2010 @ 11:39 PM

  3. Gacha
    Gacha

    You'r right, it works. But I didn't see any warning when I used _delete option.

    May 27th, 2010 @ 06:35 AM

  4. Timothy Jones
    Timothy Jones
    1. There was no deprecation warning in 2.3.5 that I ever saw.

    2. The documentation still says "delete", not "destroy".

    3. Something like this should have been in the release notes for 2.3.6-2.3.8.

    June 2nd, 2010 @ 09:14 PM

  5. Alex Rice
    Alex Rice

    Can someone point me at the deprecation warning, or what exactly happened here? Unclear if this applies to nested attributes, or the delete method in general. I also got bit by this in my 2.3.8 upgrade. Thanks

    June 17th, 2010 @ 10:38 PM

  6. José Valim
    José Valim
    • State changed from new to invalid

    Timothy, I remember we did have a deprecation warning on 2.3.5 but I completely agree with you that we should fix the documentation and it should have been included in the release notes.

    That said, could someone please fix the documentation in the required places? You can push directly to github.com/lifo/docrails (everyone has commit access). Thanks very much!

    June 21st, 2010 @ 10:40 AM

  7. Kevin Menard
    Kevin Menard
    • Importance changed from to Low

    There was definitely no deprecation message in 2.3.5 and we shouldn't be removing things we deprecate in one point release and then remove it in the next point release. It defies all release management convention.

    July 13th, 2010 @ 01:23 PM

  8. af001
    af001

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

    May 5th, 2011 @ 02:56 AM