diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 92852375ec58791f9da3bcddd455e96ac74d3489..1e2907c50b4d91027fb6c987044e59629665e484 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Alias association #build to #new so it behaves predictably. #8787 [lifofifo] + * Add notes to documentation regarding attr_readonly behavior with counter caches and polymorphic associations. Closes #9835 [saimonmoore, rick] * Observers can observe model names as symbols properly now. Closes #9869 [queso] diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 62839c46378f3076d0b1bc5f8b71b1d5eb8d94bb..5bb306c060b159363f608ef44b7a089d5ea43afe 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -88,6 +88,7 @@ def delete(*records) def build(attrs = nil) raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, @reflection.through_reflection) end + alias_method :new, :build def create!(attrs = nil) @reflection.klass.transaction do diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb index a10c1b156cc0a78927949e69952fc8e6f3cbfb4c..6b67b156642769f07edf8fcbff428ac78e587058 100644 --- a/activerecord/test/associations/join_model_test.rb +++ b/activerecord/test/associations/join_model_test.rb @@ -417,6 +417,7 @@ def test_raise_error_when_adding_new_record_to_has_many_through assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags << tags(:general).clone } assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).clone.tags << tags(:general) } assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.build } + assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.new } end def test_create_associate_when_adding_to_has_many_through