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

Ticket #8713: 0001-Fix-regression-in-r7076-to-save-built-records-on-an.patch

File 0001-Fix-regression-in-r7076-to-save-built-records-on-an.patch, 3.0 kB (added by shoe, 8 months ago)

test and fix for regression to save records built on unloaded association

  • a/activerecord/lib/active_record/associations.rb

    old new  
    986986            if association.respond_to?(:loaded?) 
    987987              if new_record? 
    988988                association 
    989               else 
     989              elsif association.loaded? 
    990990                association.select { |record| record.new_record? } 
     991              else 
     992                association.target.select { |record| record.new_record? } 
    991993              end.each do |record| 
    992994                errors.add "#{association_name}" unless record.valid? 
    993995              end 
     
    10041006              association 
    10051007            elsif association.respond_to?(:loaded?) && association.loaded? 
    10061008              association.select { |record| record.new_record? } 
     1009            elsif association.respond_to?(:loaded?) && !association.loaded? 
     1010              association.target.select { |record| record.new_record? } 
    10071011            else 
    10081012              [] 
    10091013            end 
    10101014 
    10111015            records_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank? 
    1012              
     1016 
    10131017            # reconstruct the SQL queries now that we know the owner's id 
    10141018            association.send(:construct_sql) if association.respond_to?(:construct_sql) 
    10151019          end_eval 
  • a/activerecord/test/associations_test.rb

    old new  
    687687    assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    688688  end 
    689689 
     690  def test_save_without_validations_after_build_without_loading_association 
     691    first_topic = topics(:first) 
     692    Reply.column_names 
     693 
     694    assert_equal 1, first_topic.replies.size 
     695    new_reply = nil 
     696 
     697    assert_no_queries do 
     698      new_reply = first_topic.replies.build(:title => "Not saved", :content => "Superstars") 
     699      assert new_reply.new_record? 
     700      assert_equal 2, first_topic.replies.size 
     701    end 
     702    assert first_topic.save(false) 
     703    assert !new_reply.new_record? 
     704    assert !first_topic.replies.loaded? 
     705 
     706    assert_equal 2, first_topic.replies.to_ary.size 
     707  end 
     708 
    690709  def test_build_without_loading_association 
    691710    first_topic = topics(:first) 
    692711    Reply.column_names