This project is archived and is in readonly mode.

#5105 open
Georg Ledermann

has_many: Dependent deleting does not work with STI

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

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)

Comments and changes to this ticket

  • Georg Ledermann

    Georg Ledermann July 13th, 2010 @ 11:24 AM

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

    Georg Ledermann July 13th, 2010 @ 11:29 AM

    • Tag changed from 2.3.8, has_many, patch to 2-3-stable, 2.3.8, activerecord, associations, has_many, patch
  • Neeraj Singh

    Neeraj Singh July 13th, 2010 @ 11:43 AM

    • Importance changed from “” to “Low”

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

  • Georg Ledermann

    Georg Ledermann July 13th, 2010 @ 12:12 PM

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

  • Neeraj Singh

    Neeraj Singh July 13th, 2010 @ 04:46 PM

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

    Patch does not have a valid e-mail address.
    
  • Georg Ledermann

    Georg Ledermann July 14th, 2010 @ 07:44 AM

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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

    • State changed from “open” to “stale”
  • Mandaryn

    Mandaryn March 7th, 2011 @ 09:43 PM

    • 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

  • Mandaryn

    Mandaryn March 7th, 2011 @ 10:31 PM

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

    the patch no longer applies to rails master

  • Mandaryn

    Mandaryn March 8th, 2011 @ 10:41 AM

    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

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>

Attachments

Pages