aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-10-09 16:08:11 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-10-09 16:08:11 +0100
commit68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7 (patch)
tree8c3aa0b85df3179b91b25082b41a59b0b670a37d /activerecord/lib/active_record
parente94caf0788df87b139e575f33cdeea12b06f2609 (diff)
downloadrails-68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7.tar.gz
rails-68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7.tar.bz2
rails-68d416a58fb5a47df2365c4f3a6da9f8db5c7cb7.zip
Add a :limit option to specify the maximum number of records that can be processed by accepts_nested_attributes_for
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb15
1 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index 05c7e9713f..edcf547e01 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -3,6 +3,9 @@ require 'active_support/core_ext/object/try'
module ActiveRecord
module NestedAttributes #:nodoc:
+ class TooManyRecords < ActiveRecordError
+ end
+
extend ActiveSupport::Concern
included do
@@ -203,6 +206,12 @@ module ActiveRecord
# do not have a <tt>_destroy</tt> value that evaluates to true.
# Passing <tt>:all_blank</tt> 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 => 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, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
end
+ if options[:limit] && attributes_collection.size > options[:limit]
+ raise TooManyRecords, "Maximum #{options[:limit]} records are allowed. Got #{attributes_collection.size} records instead."
+ end
+
if attributes_collection.is_a? Hash
attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes }
end