Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

date_select with :discard_day => true doesn't allow for NULL dates

#3681

I hope this looks okay, preview is busted.

Original ticket here: http://dev.rubyonrails.org/ticket/7979

Summary from original:

I'm using the following date_select in a view:

date_select('project', 'start_at',
  :end_year => 1980, :start_year =>Date.today.year,
  :discard_day => true,
  :include_blank => true,
  :order => [:month, :year])

However, when selecting the blank (:include_blank) option, the date is not typecast to NULL as it should be - rather, it is set to 2001 (for some reason).

A debug() of params[:project] reveals that the :discard_day option causes rails to set the start_at(3i) param to "1", which then causes the entire date to not be recognized as a nil submission.

Original comment:

This appears to happen when start_at was set already somewhere in the code. For instance by a default value or by assignment triggered on the previous request. Because start_at was already set, the helper will set the day value of start_at to 1 to make sure that whatever month or year is chosen, it will be a valid date. This is normally a good assumption, but not when you want to allow blank.

When these values eventually end up in ActiveRecord the automated type casting on multiparameter assign ends up calling Time.local(1) to set start_at. Which happens to be Mon Jan 01 00:00:00 +0100 2001. So it interprets the day value as a year value. This causes two problems; ActiveRecord can't do validation on the time anymore, but it also uses the parameter for the wrong argument.

Reported by Michael Guterl · January 10th, 2010 @ 08:16 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Łukasz Strzałkowski
    Łukasz Strzałkowski

    Can't reproduce it. Works properly for me. When I submit form with date selects, params have empty values as expected:

    --- !map:ActiveSupport::HashWithIndifferentAccess 
    start_at(3i): ""
    start_at(2i): ""
    start_at(1i): ""
    

    June 28th, 2010 @ 06:28 PM

  2. Aditya Sanghi
    Aditya Sanghi
    • State changed from new to resolved
    • Importance changed from to Low

    All blanks set stuff to nil, confirmed on Rails 3. closing this ticket.

    October 17th, 2010 @ 07:46 PM

  3. bingbing