aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGraeme Porteous <graeme@rgbp.co.uk>2009-07-11 20:04:18 +0200
committerEloy Duran <eloy.de.enige@gmail.com>2009-09-12 16:03:32 +0200
commitc01be9de322ba846923340e41e69821d01541610 (patch)
treee73d2aea62c53df7d1373f011ecfcd9abb70ae43
parent3091252abaafd15bc085f0be2b17829bebb6522c (diff)
downloadrails-c01be9de322ba846923340e41e69821d01541610.tar.gz
rails-c01be9de322ba846923340e41e69821d01541610.tar.bz2
rails-c01be9de322ba846923340e41e69821d01541610.zip
Fix has_one with foreign_key and primary_key association bug which caused the associated object being lost when saving the owner. [#1756 state:resolved]
Mixed in a bit from patch by ransom-briggs. [#2813 state:resolved] Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
-rw-r--r--activerecord/lib/active_record/autosave_association.rb9
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb9
-rw-r--r--activerecord/test/cases/reflection_test.rb4
-rw-r--r--activerecord/test/fixtures/accounts.yml1
-rw-r--r--activerecord/test/models/company.rb1
-rw-r--r--activerecord/test/schema/schema.rb1
6 files changed, 20 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 10dd0b4f05..75c49ecb2b 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -314,9 +314,12 @@ module ActiveRecord
if autosave && association.marked_for_destruction?
association.destroy
- elsif autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave)
- association[reflection.primary_key_name] = id
- association.save(!autosave)
+ else
+ key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
+ if autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != key || autosave)
+ association[reflection.primary_key_name] = key
+ association.save(!autosave)
+ end
end
end
end
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 7140de77ea..cdac86a3b9 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -36,6 +36,15 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal accounts(:rails_core_account), firm.account_using_primary_key
end
+ def test_update_with_foreign_and_primary_keys
+ firm = companies(:first_firm)
+ account = firm.account_using_foreign_and_primary_keys
+ assert_equal Account.find_by_firm_name(firm.name), account
+ firm.save
+ firm.reload
+ assert_equal account, firm.account_using_foreign_and_primary_keys
+ end
+
def test_can_marshal_has_one_association_with_nil_target
firm = Firm.new
assert_nothing_raised do
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb
index aced946b1e..0eb2da720e 100644
--- a/activerecord/test/cases/reflection_test.rb
+++ b/activerecord/test/cases/reflection_test.rb
@@ -176,9 +176,9 @@ class ReflectionTest < ActiveRecord::TestCase
def test_reflection_of_all_associations
# FIXME these assertions bust a lot
- assert_equal 34, Firm.reflect_on_all_associations.size
+ assert_equal 35, Firm.reflect_on_all_associations.size
assert_equal 26, Firm.reflect_on_all_associations(:has_many).size
- assert_equal 8, Firm.reflect_on_all_associations(:has_one).size
+ assert_equal 9, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end
diff --git a/activerecord/test/fixtures/accounts.yml b/activerecord/test/fixtures/accounts.yml
index b2d0191900..32583042a8 100644
--- a/activerecord/test/fixtures/accounts.yml
+++ b/activerecord/test/fixtures/accounts.yml
@@ -2,6 +2,7 @@ signals37:
id: 1
firm_id: 1
credit_limit: 50
+ firm_name: 37signals
unknown:
id: 2
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index d69152ec34..b1a3930e4e 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -78,6 +78,7 @@ class Firm < Company
# added order by id as in fixtures there are two accounts for Rails Core
# Oracle tests were failing because of that as the second fixture was selected
has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account", :order => "id"
+ has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account"
has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 9ab4cf6f43..15e5e12d03 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -22,6 +22,7 @@ ActiveRecord::Schema.define do
# unless the ordering matters. In which case, define them below
create_table :accounts, :force => true do |t|
t.integer :firm_id
+ t.string :firm_name
t.integer :credit_limit
end