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

  • SEARCH
  • Home
  • 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 7656369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:44:49+00:00 2026-05-31T12:44:49+00:00

If I pass a String into Datetime column while creating new AR object, it

  • 0

If I pass a String into Datetime column while creating new AR object, it will be automatically parse:

1.9.2p290 :011 > Movie.new(:release_date=>"21-Nov-1990")
 => #<Movie id: nil, release_date: "1990-11-21 00:00:00", created_at: nil, updated_at: nil>

How does Rails, or ActiveRecord, do this magic? Which method does it use?

  • 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-31T12:44:50+00:00Added an answer on May 31, 2026 at 12:44 pm

    Rails adds a to_date method to String. Its source is simple:

    # File activesupport/lib/active_support/core_ext/string/conversions.rb, line 42
    def to_date
      return nil if self.blank?
      ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
    end
    

    Date._parse is native to Ruby (the same method is called by Date.parse) and it’s where the real work is done.

    It first uses a regular expression to remove extraneous symbols from the string, then passes it to other methods like _parse_eu, _parse_iso, _parse_dot and so on. Each of these uses its own regular expressions and other methods to see if it’s a date that it understands and extract the meaningful information from it. Once one of them “works” (i.e. returns true), the rest are skipped. Finally, back in _parse, the extracted information is used to build a date and time, doing a little more work to figure out things like checking for the day of the week and whether a year value of “12” should mean 1912 or 2012.

    The docs call this a heuristic method, which could be taken to mean it throws a bunch of possibilities at the wall to see what sticks. It’s pretty poorly-documented but works remarkably well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass a string object into a View: <%@ Page Title= Language=C#
I'm trying to pass in a Base64 string into a C#.Net web application via
I'm looking for a way to pass a variable or string or anything into
i want to pass a string from an input box that after submition automatically
Trying to pass a string argument to a function, which will then be used
I am trying to convert a string into datetime with the following C# code,
If only string is allowed to pass into function process(), what should we do
Summary: When attempting to use marshalling to pass string data into a C++ DLL
I want to convert a time string say '12:05 PM' into a datetime using
Getting a crash when trying to pass the string into NSDateFormatter dateFromString method. I

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.