This appears to be due to something in associations.rb where the
association array is cleared as part of destroying the record. What
appears to be happening is that when person.rb is loaded, its HABTM
causes team.rb to be loaded which ends up reloading person.rb? I’ve
added some instrumentation that shows that the
destroy_without_habtm_shim_for_teams method ends up being added twice.
This causes recursion when the record is destroyed. I’ve worked around
this by embedding this method aliasing inside an “unless
method_defined?()” to prevent the recursive situation. However, I’m
suspecting that I’m missing something else obvious.
CREATE TABLE people_teams (person_id INT, team_id INT);
recursive stack sequence that looks something like:
added some instrumentation that shows that the
I changed the implementation of has_and_belongs_to_many() in
associations.rb
(gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb) to
check for the existence of the shim method:
# Don't use a before_destroy callback since users'
before_destroy
# callbacks will be executed after the association is wiped out.
old_method =
“destroy_without_habtm_shim_for_#{association_name}”
unless method_defined?(old_method) # !!!
class_eval <<-end_eval
alias_method :#{old_method}, :destroy_without_callbacks
def destroy_without_callbacks
#{association_name}.clear
#{old_method}
end
end_eval
end # !!!
I added the two lines with ‘# !!!’.
This feels more like a band-aid than a fix since I would expect this
has_and_belongs_to_many call to not occur twice to begin with. Just
haven’t had time track it down further yet.
Eric
Brian G. wrote:
Hi Eric, I’m running into the same problem. Could you provide a little
more
detail on your workaround? Thanks,