Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

No delete for you today

Andreas Maier
Thursday, January 10, 2013

Interestings

Don’t use delete_all

Let’s say you have a has_many relationship – project has_many stories. You don’t have dependent: destroy on the association.

If you call project.stories.delete_all, it does not delete the stories. Instead, it inserts NULL into the stories’ project_id field.

As a bonus, if the project_id field in the stories table has a not null constraint, then all of the project_ids become 0 – potentially attaching themselves to a completely different project.

delete_all works properly if the association has a dependent: destroy clause, and destroy_all works properly in either case.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Add New Comment Cancel reply

Your email address will not be published.

Andreas Maier

Andreas Maier
San Francisco

Recent Posts

  • Rails dies again
Subscribe to Andreas's Feed

Author Topics

  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >