This project is archived and is in readonly mode.

#4707 ✓invalid
Gacha

accepts_nested_attributes_for '_delete' feature broken in Rails 2.3.8

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

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
=> []

Comments and changes to this ticket

  • Phil C

    Phil C May 26th, 2010 @ 11:37 PM

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

  • Phil C

    Phil C May 26th, 2010 @ 11:39 PM

    eg:

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

    Gacha May 27th, 2010 @ 06:35 AM

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

  • Timothy Jones

    Timothy Jones June 2nd, 2010 @ 09:14 PM

    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.

  • Alex Rice

    Alex Rice June 17th, 2010 @ 10:38 PM

    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

  • José Valim

    José Valim June 21st, 2010 @ 10:40 AM

    • 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!

  • Kevin Menard

    Kevin Menard July 13th, 2010 @ 01:23 PM

    • 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.

  • af001

    af001 May 5th, 2011 @ 02:56 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages