---
title: Using Before Destroy and Has Many in Rails
description: A quick tip for utilizing a before_destroy callback in Rails alongside associations.
image: https://blog.madebylotus.com/hubfs/active-record-callbacks.jpg
---

![Lotus Apps](https://blog.madebylotus.com/hs-fs/hubfs/Backgrounds_Lotus%20BLog_Jan2017/logo-color.png?width=170&name=logo-color.png "Lotus Apps")

- [Work](https://www.madebylotus.com/software-solutions/)
- [Why Lotus](https://www.madebylotus.com/custom-software-development/)
- [About](https://www.madebylotus.com/about/)
- [Blog](https://blog.madebylotus.com/blog)
- [Start a Project](https://info.madebylotus.com/start-your-custom-software-solution)

# Using Before Destroy and Has Many in Rails

# Using Before Destroy and Has Many in Rails

Aug 03, 2017 | [By Matt](https://blog.madebylotus.com/blog/author/matt) |

[Ruby on Rails](https://blog.madebylotus.com/blog/topic/ruby-on-rails), [Development](https://blog.madebylotus.com/blog/topic/development)

A quick tip for utilizing a before\_destroy callback in Rails alongside associations.

## The Problem

It's not often we make use of "before\_destroy" callbacks in Rails (in fact, we advocate for service objects); however, we found ourselves wanting to make use of this callback recently to process data in a has many relationship.

### Archive Association on Destroy

In the following example, we want to perform some logic on any associated models using an `ActiveJob` worker, as well as ensure that each payment method is updated to remove the reference to this parent model.

With this code however, when our callback is executed, `payment_methods` will be empty, as the `dependent: :nullify` has already set the foreign key value to `null` for each.

 

### Prepend the Callback

As it turns out, the `dependent` option on a `has_many` association is implemented as a callback itself, a `before_destroy` to be exact. So while we could write a custom `before_action` that also handles the nullification of the foreign key, we couldn't easily reuse this existing service.

In this scenario, we simply `prepend`our callback so it is executed before foreign key nullification occurs.

- [Tweet](https://twitter.com/share)

### Written by [Matt](https://blog.madebylotus.com/blog/author/matt)

<https://blog.madebylotus.com/blog/author/matt>

Dad, husband, and Founder of Lotus Apps providing Rails and RubyMotion development out of Knoxville TN. Creator of http://www.gemprospector.com for #ruby devs.

Find me on:

[LinkedIn](https://www.linkedin.com/in/mbrewerdev) [Twitter](https://twitter.com/macfanatic)

### Subscribe to Email Updates

### Posts by Topic

- [Business (20)](https://blog.madebylotus.com/blog/topic/business)
- [Entrepreneurship (20)](https://blog.madebylotus.com/blog/topic/entrepreneurship)
- [Development (12)](https://blog.madebylotus.com/blog/topic/development)
- [Programming (9)](https://blog.madebylotus.com/blog/topic/programming)
- [Design (8)](https://blog.madebylotus.com/blog/topic/design)
- [Ruby on Rails (8)](https://blog.madebylotus.com/blog/topic/ruby-on-rails)
- [Marketing (7)](https://blog.madebylotus.com/blog/topic/marketing)
- [Ruby (6)](https://blog.madebylotus.com/blog/topic/ruby)
- [Branding (5)](https://blog.madebylotus.com/blog/topic/branding)
- [IoT (5)](https://blog.madebylotus.com/blog/topic/iot)
- [Linux (3)](https://blog.madebylotus.com/blog/topic/linux)
- [Raspberry Pi (3)](https://blog.madebylotus.com/blog/topic/raspberry-pi)
- [UI/UX (3)](https://blog.madebylotus.com/blog/topic/ui-ux)
- [Typography (2)](https://blog.madebylotus.com/blog/topic/typography)
- [Networking (1)](https://blog.madebylotus.com/blog/topic/networking)

see all

[![Web vs Mobile software solutions](https://no-cache.hubspot.com/cta/default/2735611/9f9c26bf-7589-464e-b3ed-fd96cb095788.png)](https://cta-redirect.hubspot.com/cta/redirect/2735611/9f9c26bf-7589-464e-b3ed-fd96cb095788)

[![Pro](https://no-cache.hubspot.com/cta/default/2735611/524caa0c-d5c7-4692-b2f3-60bcd646c77c.png)](https://cta-redirect.hubspot.com/cta/redirect/2735611/524caa0c-d5c7-4692-b2f3-60bcd646c77c)

[![Why Should I Brand My Company?](https://no-cache.hubspot.com/cta/default/2735611/fc55f924-34af-48ce-8497-80215293dcb2.png)](https://cta-redirect.hubspot.com/cta/redirect/2735611/fc55f924-34af-48ce-8497-80215293dcb2)

- [Expanding Objects in Stripe API](https://blog.madebylotus.com/blog/expanding-objects-in-stripe-api)
- [Announcing School PayIt](https://blog.madebylotus.com/blog/announcing-school-payit)

### Our Products

[![](https://blog.madebylotus.com/hubfs/Backgrounds_Lotus%20BLog_Jan2017/tipcounter-footer1.jpg) Tip Counter for iOS](http://www.tipcounterapp.com/)

[![](https://blog.madebylotus.com/hubfs/Backgrounds_Lotus%20BLog_Jan2017/prospector-footer2.jpg) Prospector - Monitor Your Ruby Codebase](http://www.gemprospector.com/)

[![](https://blog.madebylotus.com/hubfs/Backgrounds_Lotus%20BLog_Jan2017/brailled-footer3.jpg) BraillED for iOS](http://www.brailledapp.com/)

# Ready to have a chat?

Contact us to chat with our founder  
 so we can learn about you and your project.

 [![TELL US ABOUT YOUR PROJECT](https://no-cache.hubspot.com/cta/default/2735611/90f21417-3a88-4469-9887-821c3f2db9e1.png)](https://cta-redirect.hubspot.com/cta/redirect/2735611/90f21417-3a88-4469-9887-821c3f2db9e1)

[or call (865)-240-3801](tel:865-240-3801)

![](https://blog.madebylotus.com/hubfs/Backgrounds_Lotus%20BLog_Jan2017/lotus-footer-logo.png)

[sales@madebylotus.com](mailto:sales@madebylotus.com)

[(865)-240-3801](tel:865-240-3801)

[Contact Us](https://www.madebylotus.com/contact/)