Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 213923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:19:30+00:00 2026-05-11T18:19:30+00:00

When using Rails date_select with :prompt => true I see some very strange behavior

  • 0

When using Rails date_select with :prompt => true I see some very strange behavior when submitting the form without all fields selected. Eg.

Submitting the form with January selected but the day and year fields left at the default prompt results in January 1st 0001 getting passed to the model validation. If validation fails and the form is rendered again January is still selected (correctly) but the day is set to 1 (incorrectly). If the form is submitted with just the year selected, both month and day get set to 1.

This is very strange behavior – can anyone give me a workaround?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:19:30+00:00Added an answer on May 11, 2026 at 6:19 pm

    The problem has to do with multiparameter assignment. Basically you want to store three values into one attribute (ie. written_at). The date_select sends this as { 'written_at(1)' => '2009', 'written_at(2)' => '5', 'written_at(3)' => '27' } to the controller. Active record packs these three values into a string and initializes a new Date object with it.

    The problem starts with the fact that Date raises an exception when you try to instantiate it with an invalid date, Date.new(2009, 0, 1) for instance. Rails catches that error and instantiates a Time object instead. The Time class with timezone support in Rails has all kinds of magic to make it not raise with invalid data. This makes your day turn to 1.

    During this process active record looses the original value hash because it packed the written_at stuff into an array and tried to create a Date or Time object out of it. This is why the form can’t access it anymore using the written_at_before_time_cast method.

    The workaround would be to add six methods to your model: written_at_year, written_at_year=, and written_at_year_before_type_cast (for year, month and day). A before_validation filter can reconstruct the date and write it to written_at.

    class Event < ActiveRecord::Base
      before_validation :reconstruct_written_at
    
      def written_at_year=(year)
        @written_at_year_before_type_cast = year
      end
    
      def written_at_year
        written_at_year_before_type_cast || written_at.year
      end
    
      def written_at_year_before_type_cast
        @written_at_year_before_type_cast
      end
    
      private
    
      def reconstruct_written_at
        written_at = Date.new(written_at_year, written_at_month, written_at_day)
      rescue ArgumentError
        written_at = nil
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 153k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, they're on the stack. You can see this by… May 12, 2026 at 10:25 am
  • Editorial Team
    Editorial Team added an answer Ok, this is my solution using above classes. I added… May 12, 2026 at 10:25 am
  • Editorial Team
    Editorial Team added an answer You want the tun/tap device: http://en.wikipedia.org/wiki/TUN/TAP May 12, 2026 at 10:25 am

Related Questions

When using Rails date_select with :prompt => true I see some very strange behavior
I am currently trying to program my first ajax interface using Rails. The application
I want to let users specify a date that may or may not include
Update : This gem DOES install with sudo rake gems:install. The problem is with
When i deploy a rails application in production mode, it appends a date-time string

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.