From cfbd790ad3dbb9a5b8ee18b4e386abb412246d9e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 16 Oct 2007 07:24:23 +0000 Subject: Fix regression where the association would not construct new finder SQL on save causing bogus queries for "WHERE owner_id = NULL" even after owner was saved. Closes #8713. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7942 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations.rb | 8 ++++---- activerecord/test/associations_test.rb | 10 ++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8a3331a0ff..0b174f1e78 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix regression where the association would not construct new finder SQL on save causing bogus queries for "WHERE owner_id = NULL" even after owner was saved. #8713 [Bryan Helmkamp] + * Refactor association create and build so before & after callbacks behave consistently. #8854 [lifofifo, mortent] * Quote table names. Defaults to column quoting. #4593 [Justin Lynn, gwcoffey, eadz, Dmitry V. Sabanin, Jeremy Kemper] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 3bf660f7b2..0d440a75fd 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1083,10 +1083,10 @@ module ActiveRecord [] end - if !records_to_save.blank? - records_to_save.each { |record| association.send(:insert_record, record) } - association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id - end + records_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank? + + # reconstruct the SQL queries now that we know the owner's id + association.send(:construct_sql) if association.respond_to?(:construct_sql) end_eval # Doesn't use after_save as that would save associations added in after_create/after_update twice diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index f912816f33..03431049ae 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -14,6 +14,8 @@ require 'fixtures/author' require 'fixtures/comment' require 'fixtures/tag' require 'fixtures/tagging' +require 'fixtures/person' +require 'fixtures/reader' class AssociationsTest < Test::Unit::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects, @@ -24,6 +26,14 @@ class AssociationsTest < Test::Unit::TestCase Class.new(ActiveRecord::Base).has_many(:wheels, :name => 'wheels') end end + + def test_should_construct_new_finder_sql_after_create + person = Person.new + assert_equal [], person.readers.find(:all) + person.save! + reader = Reader.create! :person => person, :post => Post.new(:title => "foo", :body => "bar") + assert_equal [reader], person.readers.find(:all) + end def test_force_reload firm = Firm.new("name" => "A New Firm, Inc") -- cgit v1.2.3