Created
September 30, 2010 20:35
-
-
Save purcell/605268 to your computer and use it in GitHub Desktop.
This initializer patches the Rails bug described in ticket 5674
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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