From ae74e8e9feba99b84f5e431239dc6a2039ad5793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Wed, 16 Aug 2006 16:05:48 +0000 Subject: Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4773 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../active_record/associations/association_proxy.rb | 19 +++++++++---------- activerecord/test/associations_test.rb | 10 ++++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 99949ff510..f5813a8d03 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. [Tobias Luetke] + * Nested classes are given table names prefixed by the singular form of the parent's table name. [Jeremy Kemper] Example: Invoice::Lineitem is given table name invoice_lineitems diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index a94daccd34..c093e40ca0 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -119,21 +119,20 @@ module ActiveRecord private def method_missing(method, *args, &block) - load_target - @target.send(method, *args, &block) + if load_target + @target.send(method, *args, &block) + end end def load_target - if !@owner.new_record? || foreign_key_present - begin - @target = find_target unless loaded? - rescue ActiveRecord::RecordNotFound - reset - end + if !loaded? and (!@owner.new_record? || foreign_key_present) + @target = find_target end - loaded - target + @loaded = true + @target + rescue ActiveRecord::RecordNotFound + reset end # Can be overwritten by associations that might have the foreign key available for an association without diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index f99322d976..cb704ffd2e 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -313,6 +313,16 @@ class HasOneAssociationsTest < Test::Unit::TestCase firm.account = Account.find(:first).clone assert_queries(2) { firm.save! } end + + def test_save_still_works_after_accessing_nil_has_one + jp = Company.new :name => 'Jaded Pixel' + jp.dummy_account.nil? + + assert_nothing_raised do + jp.save! + end + end + end -- cgit v1.2.3