Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The has_many create method works with polymorphic associations. Closes

…#6361.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0c3c131f3b9a82d3e2f36b2930c3437c9f821b6d 1 parent 6c0609f
Jeremy Kemper jeremy authored
2  activerecord/CHANGELOG
View
@@ -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]
9 activerecord/lib/active_record/associations/has_many_association.rb
View
@@ -91,20 +91,21 @@ def find(*args)
@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)
7 activerecord/test/associations/join_model_test.rb
View
@@ -137,6 +137,13 @@ def test_create_polymorphic_has_many_with_scope
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
Please sign in to comment.
Something went wrong with that request. Please try again.