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

Ticket #8713: always_construct_new_sql_on_save.patch

File always_construct_new_sql_on_save.patch, 1.9 kB (added by brynary, 1 year ago)
  • test/associations_test.rb

    old new  
    1414require 'fixtures/comment' 
    1515require 'fixtures/tag' 
    1616require 'fixtures/tagging' 
     17require 'fixtures/person' 
     18require 'fixtures/reader' 
    1719 
    1820class AssociationsTest < Test::Unit::TestCase 
    1921  fixtures :accounts, :companies, :developers, :projects, :developers_projects, 
     
    2426      Class.new(ActiveRecord::Base).has_many(:wheels, :name => 'wheels') 
    2527    end 
    2628  end 
     29   
     30  def test_should_construct_new_finder_sql_after_create 
     31    person = Person.new 
     32    assert_equal [], person.readers.find(:all) 
     33    person.save! 
     34    reader = Reader.create! :person => person, :post => Post.new(:title => "foo", :body => "bar") 
     35    assert_equal [reader], person.readers.find(:all) 
     36  end 
    2737 
    2838  def test_force_reload 
    2939    firm = Firm.new("name" => "A New Firm, Inc") 
  • lib/active_record/associations.rb

    old new  
    10831083              [] 
    10841084            end 
    10851085 
    1086             if !records_to_save.blank? 
    1087               records_to_save.each { |record| association.send(:insert_record, record) } 
    1088               association.send(:construct_sql)   # reconstruct the SQL queries now that we know the owner's id 
    1089             end 
     1086            records_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank? 
     1087             
     1088            # reconstruct the SQL queries now that we know the owner's id 
     1089            association.send(:construct_sql) if association.respond_to?(:construct_sql) 
    10901090          end_eval 
    10911091 
    10921092          # Doesn't use after_save as that would save associations added in after_create/after_update twice