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.

has_many: Dependent deleting does not work with STI

#5105

has_many has a deleting/nullify bug if used with STI. Please see this example:

class Contact < ActiveRecord::Base
  has_many :addresses, :as => :object, :dependent => :delete_all
end

class Person < Contact
end
person = Person.find(42)
person.destroy

=> ... deleting contact works fine .... but:
=> DELETE FROM `addresses` WHERE ((object_id = 42) AND (from_type = 'Person'))

This is wrong. It should be:

=> DELETE FROM `addresses` WHERE ((object_id = 42) AND (from_type = 'Contact'))

This bug can be found in Rails 2.3.8. So far is I see it's in Rails 3, too.

Attached is a patch (sorry, no tests so far)

Reported by Georg Ledermann · July 13th, 2010 @ 10:18 AM

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

Activity

  1. Georg Ledermann
    Georg Ledermann
    • Tag changed from 2.3.8 has_many patch to 2.3.8, has_many, patch

    July 13th, 2010 @ 11:24 AM

  2. Georg Ledermann
    Georg Ledermann
    • Tag changed from 2.3.8, has_many, patch to 2-3-stable, 2.3.8, activerecord, associations, has_many, patch

    July 13th, 2010 @ 11:29 AM

  3. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    If this is an issue in rails3 then can you attach a patch for rails3 too?

    July 13th, 2010 @ 11:43 AM

  4. Georg Ledermann
    Georg Ledermann

    The patch for Rails3 is just the same. I've attached it.

    July 13th, 2010 @ 12:12 PM

  5. Neeraj Singh
    Neeraj Singh

    When I apply the patch (patch-rails3.txt) I get following message

    Patch does not have a valid e-mail address.
    

    July 13th, 2010 @ 04:46 PM

  6. Georg Ledermann
    Georg Ledermann
    • Tag changed from 2-3-stable, 2.3.8, activerecord, associations, has_many, patch to 2-3-stable, 2.3.8, activerecord, associations, has_many, patch, rails3

    Sorry, here comes a valid patch file for Rails 3. But there are still no tests for this very simple change. The patch just added a missing "base_class" usage.

    July 14th, 2010 @ 07:44 AM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2-3-stable, 2.3.8, activerecord, associations, has_many, patch, rails3 to 2-3-stable, 238, activerecord, associations, has_many, patch, rails3

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:36 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:36 PM

  9. Mandaryn
    Mandaryn
    • Tag changed from 2-3-stable, 238, activerecord, associations, has_many, patch, rails3 to activerecord, rails3
    • State changed from stale to open

    [state:open]

    Just got stung by this on a rails 3.0.4.

    The use case and error are exactly the same as in the original ticket.

    I will try adding some testing to the patch

    March 7th, 2011 @ 09:43 PM

  10. Mandaryn
    Mandaryn

    ok my fault here, my problem is slightly (or even more than slightly) different.

    the patch no longer applies to rails master

    March 7th, 2011 @ 10:31 PM

  11. Mandaryn
    Mandaryn

    after a bit of investigation I am convinced that this issue is fixed in current master [a0d7247d1509762283c61182ad82c2eed8d54757]

    this case is also covered by this test:

    test_clearing_an_exclusively_dependent_association_collection

    in activerecord/test/cases/associations/has_many_associations_test.rb

    March 8th, 2011 @ 10:41 AM