Skip to content

Instantly share code, notes, and snippets.

@purcell
Created September 30, 2010 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purcell/605268 to your computer and use it in GitHub Desktop.
Save purcell/605268 to your computer and use it in GitHub Desktop.
This initializer patches the Rails bug described in ticket 5674
# This initializer patches the bug described in ticket 5674:
# https://rails.lighthouseapp.com/projects/8994/tickets/5674-regression-habtm-deletion-fails-when-join-table-has-foreign-keys
if Rails.version == '3.0.0'
module ActiveRecord::Associations
autoload :HasAndBelongsToManyAssociation, 'active_record/associations/has_and_belongs_to_many_association'
module ClassMethods
def has_and_belongs_to_many(association_id, options = {}, &extension)
reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension)
collection_accessor_methods(reflection, HasAndBelongsToManyAssociation)
# Don't use a before_destroy callback since users' before_destroy
# callbacks will be executed after the association is wiped out.
include Module.new {
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def destroy # def destroy
#{reflection.name}.clear # posts.clear
super # super
end # end
RUBY
}
add_association_callbacks(reflection.name, options)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment