aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb9
-rw-r--r--activerecord/test/associations/join_model_test.rb7
3 files changed, 14 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 9524643113..f2b377a20b 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* The has_many create method works with polymorphic associations. #6361 [Dan Peterson]
+
* MySQL: introduce Mysql::Result#all_hashes to support further optimization. #5581 [Stefan Kaes]
* save! shouldn't validate twice. #6324 [maiha, Bob Silva]
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index d0af68eea7..3cd6e5d716 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -91,20 +91,21 @@ module ActiveRecord
@reflection.klass.find(*args)
end
end
-
+
protected
def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
else
+ create_scoping = {}
+ set_belongs_to_association_for(create_scoping)
+
@reflection.klass.with_scope(
+ :create => create_scoping,
:find => {
:conditions => @finder_sql,
:joins => @join_sql,
:readonly => false
- },
- :create => {
- @reflection.primary_key_name => @owner.id
}
) do
@reflection.klass.send(method, *args, &block)
diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb
index fb8df9c21c..c67956fba3 100644
--- a/activerecord/test/associations/join_model_test.rb
+++ b/activerecord/test/associations/join_model_test.rb
@@ -137,6 +137,13 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
assert_equal "Post", tagging.taggable_type
assert_equal old_count+1, posts(:welcome).taggings.count
end
+
+ def test_create_bang_polymorphic_with_has_many_scope
+ old_count = posts(:welcome).taggings.count
+ tagging = posts(:welcome).taggings.create!(:tag => tags(:misc))
+ assert_equal "Post", tagging.taggable_type
+ assert_equal old_count+1, posts(:welcome).taggings.count
+ end
def test_create_polymorphic_has_one_with_scope
old_count = Tagging.count