Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10556 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Fixing ActiveRecord::MultiparameterAssignmentErrors thrown when a model with a Date is assigned an invalid date via multiparameter attributes

Reported by: leikind Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: Date, MultiparameterAssignmentErrors verified
Cc:

Description

Problem

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_multip(EUR 0.05) arameter_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'

Following the Rails way creating a new ActiveRecord instance should not raise exceptions if input data is invalid!

The cause of this behavior is the Date class throwing ArgumentError when an attempt is made to create an invalid Date. This differs from the behavior of Time which will convert November 31 to December 1.

Solution

The proposed solution is:

1) Intercept ArgumentError if the class is Date (and only Date)

2) If ArgumentError is raised, create a Time object using the same parameters

3) Convert it to Date

All that it does is making it consistent with the behavior already present in Rails , that is, with those cases when we have a Time field and invalid parameters are sent from the form to create an model instance with such a Time field, so why don't we follow the same pattern with dates?

There is already a discusion of this issue here - http://dev.rubyonrails.org/ticket/10307

Attachments

fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes.diff (2.9 kB) - added by leikind on 12/18/07 23:04:37.
patch for the invalid date and ActiveRecord::MultiparameterAssignmentErrors problem
fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes2.diff (2.8 kB) - added by leikind on 12/22/07 13:35:57.
I removed some redundant comment from the test

Change History

12/18/07 23:04:37 changed by leikind

  • attachment fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes.diff added.

patch for the invalid date and ActiveRecord::MultiparameterAssignmentErrors problem

12/19/07 15:15:22 changed by danielmorrison

  • cc set to danielmorrison.

12/19/07 15:24:28 changed by danielmorrison

  • cc deleted.

+1

I like that it duplicates the functionality of Time.

12/19/07 16:03:06 changed by evolving_jerk

+1

12/20/07 21:37:50 changed by brandon

  • keywords changed from Date, MultiparameterAssignmentErrors to Date, MultiparameterAssignmentErrors verified.

+1 agree that it should work the same as Time

12/22/07 13:35:57 changed by leikind

  • attachment fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes2.diff added.

I removed some redundant comment from the test

02/02/08 03:37:30 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8777]) When multiparameter date assignment fails due to an invalid date, fall back to create a Time and convert to_date. Closes #10556 [leikind]

02/10/08 01:10:47 changed by nzkoz

(In [8839]) 2-0-stable: When multiparameter date assignment fails due to an invalid date, fall back to create a Time and convert to_date. References #10556 [leikind]

Merging [8777]