If the user has chosen an invalid date in the standard Rails date helper (i.e. 31 of February), creating a new ActiveRecord instance will lead to ActiveRecord::MultiparameterAssignmentErrors
Example:
Suppose CalendarEvent model has a date field called start_date.
Running this line:
CalendarEvent.new({"start_date(1i)"=>"2007", "start_date(2i)"=>"11", "start_date(3i)"=>"31"})
will lead to this:
ActiveRecord::MultiparameterAssignmentErrors: 1 error(s) on assignment of multiparameter attributes
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:2097:in `execute_callstack_for_multiparameter_attributes'
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:2077:in `assign_multiparameter_attributes'
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1678:in `attributes='
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1508:in `initialize_without_callbacks'
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/callbacks.rb:225:in `initialize'
This stacktrace belongs to Rails 2.0 RC2 ( http://dev.rubyonrails.org/svn/rails/tags/rel_2-0-0_RC2/ ) but has existed for a while in older versions.
Creating a new ActiveRecord instance should not raise exceptions if input data is invalid, this should happen when validating the object!
The solution has also been around for a while in the form of a plugin - http://wiki.rubyonrails.org/rails/pages/Validates+Multiparameter+Assignments , which is actually a short fix:
module ActiveRecord
module Validations
module ClassMethods
def validates_multiparameter_assignments(options = {})
configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid] }.update(options)
alias_method :assign_multiparameter_attributes_without_rescuing, :assign_multiparameter_attributes
attr_accessor :assignment_error_attrs
define_method(:assign_multiparameter_attributes) do |pairs|
self.assignment_error_attrs = []
begin
assign_multiparameter_attributes_without_rescuing(pairs)
rescue ActiveRecord::MultiparameterAssignmentErrors
$!.errors.each do |error|
self.assignment_error_attrs << error.attribute
end
end
end
private :assign_multiparameter_attributes
validate do |record|
record.assignment_error_attrs && record.assignment_error_attrs.each do |attr|
record.errors.add(attr, configuration[:message])
end
end
end
end
end
end