Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9224

Show
Ignore:
Timestamp:
04/04/08 14:37:22 (8 months ago)
Author:
nzkoz
Message:

Make HABTM#create behave the same as << with after_add callbacks. Closes #11374 [freels]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    r8653 r9224  
    149149 
    150150      private 
    151         def create_record(attributes
     151        def create_record(attributes, &block
    152152          # Can't use Base.create because the foreign key may be a protected attribute. 
    153153          ensure_owner_is_not_new 
     
    155155            attributes.collect { |attr| create(attr) } 
    156156          else 
    157             record = build(attributes) 
    158             yield(record) 
    159             record 
     157            load_target 
     158            build_record(attributes, &block) 
    160159          end 
    161160        end 
  • trunk/activerecord/test/cases/associations/callbacks_test.rb

    r8681 r9224  
    9595  end 
    9696 
     97  def test_has_and_belongs_to_many_after_add_called_after_save 
     98    ar = projects(:active_record) 
     99    assert ar.developers_log.empty? 
     100    alice = Developer.new(:name => 'alice') 
     101    ar.developers_with_callbacks << alice 
     102    assert_equal"after_adding#{alice.id}", ar.developers_log.last 
     103 
     104    bob = ar.developers_with_callbacks.create(:name => 'bob') 
     105    assert_equal "after_adding#{bob.id}", ar.developers_log.last 
     106 
     107    ar.developers_with_callbacks.build(:name => 'charlie') 
     108    assert_equal "after_adding<new>", ar.developers_log.last 
     109  end 
     110 
     111 
    97112  def test_has_and_belongs_to_many_remove_callback 
    98113    david = developers(:david)