This project is archived and is in readonly mode.

#1282 ✓stale
Steven Soroka

has_many :dependent => :restrict

Reported by Steven Soroka | October 27th, 2008 @ 05:29 PM | in 3.x

Referential databases implementing SQL have a references option to "on delete restrict", preventing a record from being deleted if it has child records.

Rails doesn't have anything like that, and since that's exactly what I need in my project (ie, users with ledger entries should never be deleted), I'm creating a ticket for it.

Patch to follow shortly.

Comments and changes to this ticket

  • Steven Soroka
  • Marshall

    Marshall January 6th, 2009 @ 05:13 AM

    +1 on the idea. However I think there's a bug if you have other associations with :dependent => :destroy on the same model. See http://lists.rubyonrails.org/pip... for a previous discussion of the problem.

  • Marshall

    Marshall January 6th, 2009 @ 05:16 AM

    I should've posted a bit more information on the problem... if you have :dependent => :destroy on a different association that is defined before the :dependent => :restrict association, then the first association's before_destroy will fire before the restricted association raises the exception.

  • Steven Soroka

    Steven Soroka January 6th, 2009 @ 03:24 PM

    Hmm, good point, I'll have to fix that.

  • Xavier Shay
  • Xavier Shay

    Xavier Shay February 8th, 2009 @ 05:57 PM

    though it should return false rather than raise I don't quite understand Marshall's problem - all the callbacks happen in a transaction so who cares if the :destroy callbacks trigger first? (aside from maybe performance)

  • Steven Soroka

    Steven Soroka March 9th, 2009 @ 02:52 PM

    Maybe it's a problem because mysql in myisam mode doesn't support db transactions?

  • wildchild

    wildchild March 21st, 2010 @ 07:40 AM

    Quite useful. Any status update?

  • Steven Soroka

    Steven Soroka March 21st, 2010 @ 03:30 PM

    I have some time, I'll take another look at this. I think you'd be fine to use the patch as long as you have transaction support in the database.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 04:49 AM

    • State changed from “new” to “incomplete”
    • Importance changed from “” to “”

    Please update this patch to apply to current Rails master and 2-3-stable.

  • Prem Sichanugrist (sikachu)

    Prem Sichanugrist (sikachu) October 11th, 2010 @ 12:12 PM

    • Assigned user set to “Prem Sichanugrist (sikachu)”

    I think this would be better included in 3.1.

    However, do you think this feature should support non-transactional database engine too? If so, I have to find the way to make the relational checking to be the topmost callback.

  • François Beausoleil

    François Beausoleil December 20th, 2010 @ 03:45 PM

    Couldn't this simply add a validation check before anything else? Then validation will pick the problem, and before_destroy callbacks won't have a chance to fire. This should anyway be complemented with a constraint in the DB.

    I created a duplicate ticket, even though Google didn't return anything useful: https://rails.lighthouseapp.com/projects/8994/tickets/6199-has_many...

  • rails

    rails March 21st, 2011 @ 12:00 AM

    • Tag changed from 2.2, activerecord, association, has_many to 22, activerecord, association, has_many
    • State changed from “incomplete” to “open”

    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.

  • rails

    rails March 21st, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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

Referenced by

Pages