<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -3,6 +3,9 @@ require 'active_support/core_ext/object/try'
 
 module ActiveRecord
   module NestedAttributes #:nodoc:
+    class TooManyRecords &lt; ActiveRecordError
+    end
+
     extend ActiveSupport::Concern
 
     included do
@@ -203,6 +206,12 @@ module ActiveRecord
       #   do not have a &lt;tt&gt;_destroy&lt;/tt&gt; value that evaluates to true.
       #   Passing &lt;tt&gt;:all_blank&lt;/tt&gt; instead of a Proc will create a proc
       #   that will reject a record where all the attributes are blank.
+      # [:limit]
+      #   Allows you to specify the maximum number of the associated records that
+      #   can be processes with the nested attributes. If the size of the
+      #   nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
+      #   exception is raised. If omitted, any number associations can be processed.
+      #   Note that the :limit option is only applicable to one-to-many associations.
       #
       # Examples:
       #   # creates avatar_attributes=
@@ -214,7 +223,7 @@ module ActiveRecord
       def accepts_nested_attributes_for(*attr_names)
         options = { :allow_destroy =&gt; false }
         options.update(attr_names.extract_options!)
-        options.assert_valid_keys(:allow_destroy, :reject_if)
+        options.assert_valid_keys(:allow_destroy, :reject_if, :limit)
 
         attr_names.each do |association_name|
           if reflection = reflect_on_association(association_name)
@@ -334,6 +343,10 @@ module ActiveRecord
         raise ArgumentError, &quot;Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})&quot;
       end
 
+      if options[:limit] &amp;&amp; attributes_collection.size &gt; options[:limit]
+        raise TooManyRecords, &quot;Maximum #{options[:limit]} records are allowed. Got #{attributes_collection.size} records instead.&quot;
+      end
+
       if attributes_collection.is_a? Hash
         attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes }
       end</diff>
      <filename>activerecord/lib/active_record/nested_attributes.rb</filename>
    </modified>
    <modified>
      <diff>@@ -603,3 +603,33 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation &lt; ActiveRecord::Test
 
   include NestedAttributesOnACollectionAssociationTests
 end
+
+class TestNestedAttributesLimit &lt; ActiveRecord::TestCase
+  def setup
+    Pirate.accepts_nested_attributes_for :parrots, :limit =&gt; 2
+
+    @pirate = Pirate.create!(:catchphrase =&gt; &quot;Don' botharrr talkin' like one, savvy?&quot;)
+  end
+
+  def teardown
+    Pirate.accepts_nested_attributes_for :parrots, :allow_destroy =&gt; true, :reject_if =&gt; proc { |attributes| attributes.empty? }
+  end
+
+  def test_limit_with_less_records
+    @pirate.attributes = { :parrots_attributes =&gt; { 'foo' =&gt; { :name =&gt; 'Big Big Love' } } }
+    assert_difference('Parrot.count') { @pirate.save! }
+  end
+
+  def test_limit_with_number_exact_records
+    @pirate.attributes = { :parrots_attributes =&gt; { 'foo' =&gt; { :name =&gt; 'Lovely Day' }, 'bar' =&gt; { :name =&gt; 'Blown Away' } } }
+    assert_difference('Parrot.count', 2) { @pirate.save! }
+  end
+
+  def test_limit_with_exceeding_records
+    assert_raises(ActiveRecord::NestedAttributes::TooManyRecords) do
+      @pirate.attributes = { :parrots_attributes =&gt; { 'foo' =&gt; { :name =&gt; 'Lovely Day' },
+                                                      'bar' =&gt; { :name =&gt; 'Blown Away' },
+                                                      'car' =&gt; { :name =&gt; 'The Happening' }} }
+    end
+  end
+end</diff>
      <filename>activerecord/test/cases/nested_attributes_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e94caf0788df87b139e575f33cdeea12b06f2609</id>
    </parent>
  </parents>
  <author>
    <name>Pratik Naik</name>
    <login>lifo</login>
    <email>pratiknaik@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7</url>
  <id>68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7</id>
  <committed-date>2009-10-09T08:08:11-07:00</committed-date>
  <authored-date>2009-10-09T08:08:11-07:00</authored-date>
  <message>Add a :limit option to specify the maximum number of records that can be processed by accepts_nested_attributes_for</message>
  <tree>8c3aa0b85df3179b91b25082b41a59b0b670a37d</tree>
  <committer>
    <name>Pratik Naik</name>
    <login>lifo</login>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
