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

Changeset 4792

Show
Ignore:
Timestamp:
08/20/06 05:23:34 (2 years ago)
Author:
bitsweat
Message:

Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key and adds it to the correct association. Closes #5829.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4790 r4792  
    11*SVN* 
    22 
    3 * Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key.  #5815 [josh@hasmanythrough.com] 
     3* Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key and adds it to the correct association.  #5815, #5829 [josh@hasmanythrough.com] 
    44 
    55* PostgreSQL: simplify index introspection query.  #5819 [stephen_purcell@yahoo.com] 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r4791 r4792  
    6060            raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? 
    6161 
    62             @target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } 
     62            @owner.send(@reflection.through_reflection.name).proxy_target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } 
     63            @target << associate 
    6364          end 
    6465        end 
  • trunk/activerecord/test/associations_join_model_test.rb

    r4791 r4792  
    373373    count = posts(:thinking).tags.count 
    374374    push = Tag.create!(:name => 'pushme') 
    375     assert_nothing_raised { posts(:thinking).tags << push } 
    376     assert_equal(count + 1, posts(:thinking).tags.size) 
    377     assert_equal(count + 1, posts(:thinking).tags(true).size) 
    378  
    379     assert_nothing_raised { posts(:thinking).tags.create!(:name => 'foo') } 
    380     assert_equal(count + 2, posts(:thinking).tags.size) 
    381     assert_equal(count + 2, posts(:thinking).tags(true).size) 
    382  
    383     assert_nothing_raised { posts(:thinking).tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) } 
    384     assert_equal(count + 4, posts(:thinking).tags.size) 
    385     assert_equal(count + 4, posts(:thinking).tags(true).size) 
     375    post_thinking = posts(:thinking) 
     376    assert_nothing_raised { post_thinking.tags << push } 
     377    assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag }, 
     378                message = "Expected a Tag in tags collection, got #{wrong.class}.") 
     379    assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging }, 
     380                message = "Expected a Tagging in taggings collection, got #{wrong.class}.") 
     381    assert_equal(count + 1, post_thinking.tags.size) 
     382    assert_equal(count + 1, post_thinking.tags(true).size) 
     383 
     384    assert_nothing_raised { post_thinking.tags.create!(:name => 'foo') } 
     385    assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag }, 
     386                message = "Expected a Tag in tags collection, got #{wrong.class}.") 
     387    assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging }, 
     388                message = "Expected a Tagging in taggings collection, got #{wrong.class}.") 
     389    assert_equal(count + 2, post_thinking.tags.size) 
     390    assert_equal(count + 2, post_thinking.tags(true).size) 
     391 
     392    assert_nothing_raised { post_thinking.tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) } 
     393    assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag }, 
     394                message = "Expected a Tag in tags collection, got #{wrong.class}.") 
     395    assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging }, 
     396                message = "Expected a Tagging in taggings collection, got #{wrong.class}.") 
     397    assert_equal(count + 4, post_thinking.tags.size) 
     398    assert_equal(count + 4, post_thinking.tags(true).size) 
    386399  end 
    387400