Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't "before_remove" firing in this has_many association?

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"?

like image 715
Peter Nixey Avatar asked Jan 17 '12 11:01

Peter Nixey


People also ask

What is has_ many?

A has_many association is similar to has_one , but indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model.

What are rails associations?

In Rails, an association can be defined as a relationship between two Active Record models. For example, in the Granite application, a task created by a user. And each user can create multiple tasks thus building a relationship between the User and Task models.


1 Answers

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.

like image 83
shime Avatar answered Oct 30 '22 05:10

shime