Module: ActiveRecord::Associations::ClassMethods

Defined in:
lib/m_active_record_associatioins.rb

Instance Method Summary collapse

Instance Method Details

#has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object

fixes a bug with ActiveRecord, see dev.rubyonrails.org/ticket/7570



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/m_active_record_associatioins.rb', line 5

def has_and_belongs_to_many(association_id, options = {}, &extension)
  reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension)

  add_multiple_associated_save_callbacks(reflection.name)
  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.
  old_method = "destroy_without_habtm_shim_for_#{reflection.name}"
  ##class_eval <<-end_eval
  class_eval <<-end_eval  unless method_defined?(old_method) 
  alias_method :#{old_method}, :destroy_without_callbacks
  def destroy_without_callbacks
    #{reflection.name}.clear
    #{old_method}
  end
  end_eval

  add_association_callbacks(reflection.name, options)

  # deprecated api
  deprecated_collection_count_method(reflection.name)
  deprecated_add_association_relation(reflection.name)
  deprecated_remove_association_relation(reflection.name)
  deprecated_has_collection_method(reflection.name)
end