Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:45:18+00:00 2026-05-28T06:45:18+00:00

I want to run some code before an object is removed from a has_many

  • 0

I want to run some code before an object is removed from a has_many association.

I thought that I would be able to do this with the before_remove callback however for some reason this isn’t firing and I don’t understand why.

class Person < ActiveRecord::Base
  has_many :limbs, before_remove: :print_message

  def print_message
    puts 'removing a limb'
  end
end

class Limb < ActiveRecord::Base
  belongs_to :person
end

While this code should print “removing a limb” during the destruction of a limb but it doesn’t.

p = Person.create;
l = Limb.create person: p;
p.limbs.first.destroy
# SQL (2.1ms)  DELETE FROM "limbs" WHERE "limbs"."id" = ?  [["id", 5]]
# => #<Limb id: 5, person_id: 3, created_at: "2012-01-17 11:28:01", updated_at: "2012-01-17 11:28:01"> 

Why does this destroy action not cause the print_message method to run?

EDIT – does this before_remove callback exist?

A number of people have asked whether this callback exists. Although I can find very few further references to it, it is documented in the Rails documentation:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Association+callbacks

It’s an association callback though rather than a root ActiveRecord callback

Edit 2 – why not just use before_destroy on Limb?

Some people are asking why I’m not using the before_destroy callback on Limb. The reason is that I want person to check that there is a minimum number of limbs and that the last one is never destroyed. This is the original problem:
How do you ensure that has_many always "has a minimum"?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:45:19+00:00Added an answer on May 28, 2026 at 6:45 am

    before_remove callback exists as an option in Associations callbacks. It’s not the same as before_destroy, which is an ActiveRecord callback.

    This is how you use it:

    class Person < ActiveRecord::Base
      has_many :limbs, :before_remove => :print_message
    
      def print_message(limb)
        # limb variable references to a Limb instance you're removing
        # ( do some action here )
        # ...
      end
    end
    
    class Limb < ActiveRecord::Base
      belongs_to :person
    end
    

    You’re also calling a remove method incorrectly.

    p = Person.create
    l = Limb.create(:person => p)
    p.limbs.first.destroy 
    

    Here you’re calling it on Limb instance, that’s why nothing is triggered.
    Call it on an association you created:

    p = Person.create
    l = Limb.create(:person => p)
    p.limbs.destroy(l)
    

    EDIT

    For preserving minimum of associated objects you can do something like this:

    class Person < ActiveRecord::Base
      has_many :limbs, :before_remove => :preserve_mimimum
    
      def preserve_minimum(limb)
        raise "Minimum must be preserved" if limbs.count == 1
      end
    end
    
    class Limb < ActiveRecord::Base
      belongs_to :person
    end
    

    This however does not get triggered on p.limbs.destroy_all, so you have to do something like this p.limbs.each {|l| p.limbs.destroy(l)}

    Why it does not get triggered by destroy_all?

    Because of this:

    def destroy_all(conditions = nil)
       find(:all, :conditions => conditions).each { |object| object.destroy }
    end
    

    It iterates over each element of an association and executes destroy action on an object and not on an association, that’s why.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want this for some conditional compilation code that will run in all IE's
I want to run some code from an ASP.NET MVC controller action in a
I want my C# WinForms app to run some code when either the app
I often run into the situation where I want to disable some code while
I want to run Prolog code using Java. I found some engines, but the
I want to run some Python unit tests from my Maven module. I have
I have a web application that I want to run some system tests on,
I have created one ruby script that I want to run with some flags
Let me post some code before I ask question. public Object returnSomeResult() { Object
I want to reuse some code logic from a self-hosted web application in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.