#8 new
Jon Leighton

Declaring acts_as_paranoid after a habtm association causes problems

Reported by Jon Leighton | March 31st, 2008 @ 05:49 AM

Best explained with an example:

class Person < ActiveRecord::Base
  has_any_belongs_to_many :groups

The habtm assoc redefined destroy_without_callbacks to clear the association before actually destroying the record. It aliases the old destroy_without_callbacks (which at this point does an actual DELETE FROM) as destroy_without_habtm_shim_for_groups (in this case). For some reason, and I can't quite get my head around why, if acts_as_paranoid is declared after the habtm, destroy_with_callbacks will call the destroy_without_callbacks defined by the association, not the one defined by acts_as_paranoid, and so an actual DELETE FROM operation will happen.

A work around is to put acts_as_paranoid before the association.

Comments and changes to this ticket

  • mattwestcott

    mattwestcott May 8th, 2008 @ 08:12 PM

    Just been bitten by this one myself. The reason AAP's destroy_with_callbacks fails to override habtm's definition is that AAP brings it in via an 'include'; this has lower priority than habtm's definition which is done inside a class_eval.

    In any case, we wouldn't want to override destroy_with_callbacks at this point, because then we'd end up clobbering the habtm on-delete functionality. The method we really want to override is the one now known as destroy_without_habtm_shim_for_X, which is going to be tricky to pick out (especially as it might be buried under multiple habtm wrappers). I suspect the only way forward will be to monkey-patch the has_and_belongs_to_many function to make it expose the original destroy_with_callbacks with a predictable name. Still pondering my plan of attack there though.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket