From a32a9863640dbd7a74b251e5467f90d134396110 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 12 Feb 2006 18:03:43 +0000 Subject: save polymorphic joins from the has_many|one association (closes #3812) [Rick Olson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3586 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/association_proxy.rb | 9 +++++++++ .../associations/has_many_association.rb | 4 ++-- .../associations/has_one_association.rb | 3 +-- activerecord/test/associations_join_model_test.rb | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index c62c042ab2..a4fc85b15e 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -74,6 +74,15 @@ module ActiveRecord @owner.send(:extract_options_from_args!, args) end + def set_belongs_to_association_for(record) + if @reflection.options[:as] + record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? + record["#{@reflection.options[:as]}_type"] = ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s + else + record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + end + end + def merge_options_from_reflection!(options) options.reverse_merge!( :group => @reflection.options[:group], diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 6800f363dc..b861fde55d 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -13,7 +13,7 @@ module ActiveRecord else load_target record = @reflection.klass.new(attributes) - record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + set_belongs_to_association_for(record) @target << record record end @@ -140,7 +140,7 @@ module ActiveRecord end def insert_record(record) - record[@reflection.primary_key_name] = @owner.id + set_belongs_to_association_for(record) record.save end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 4dbc0f6441..d07323a869 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -42,8 +42,7 @@ module ActiveRecord @target = nil else raise_on_type_mismatch(obj) - - obj[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + set_belongs_to_association_for(obj) @target = (AssociationProxy === obj ? obj.target : obj) end diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 5437e049a3..5c1510faff 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -56,6 +56,28 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal "Post", tagging.taggable_type end + def test_set_polymorphic_has_many + tagging = tags(:misc).taggings.create + posts(:thinking).taggings << tagging + assert_equal "Post", tagging.taggable_type + end + + def test_set_polymorphic_has_one + tagging = tags(:misc).taggings.create + posts(:thinking).tagging = tagging + assert_equal "Post", tagging.taggable_type + end + + def test_create_polymorphic_has_many_with_scope + tagging = posts(:welcome).taggings.create(:tag => tags(:general)) + assert_equal "Post", tagging.taggable_type + end + + def test_create_polymorphic_has_one_with_scope + tagging = posts(:welcome).tagging.create(:tag => tags(:general)) + assert_equal "Post", tagging.taggable_type + end + def test_has_many_with_piggyback assert_equal "2", categories(:sti_test).authors.first.post_id.to_s end -- cgit v1.2.3