how to convert a hash value returned from a date_select in rails to a Date object?

前端 未结 5 1675
感动是毒
感动是毒 2020-12-18 00:50

I have a date_select in my view inside a form, however on submit the value returned is in a hash form like so:

{\"(1i)\"=>\"2010\", \"(2i)\"=>\"8\", \"         


        
5条回答
  •  悲哀的现实
    2020-12-18 01:29

    This particular code (the one that does conversion) can be tracked from lib/active_record/connection_adapters/abstract/schema_definitions.rb, line no 67 onwards, i.e. the method type_cast.

    These two methods are used to generate a date from string:

    def fast_string_to_date(string)
      if string =~ Format::ISO_DATE
        new_date $1.to_i, $2.to_i, $3.to_i
      end
    end
    
    # Doesn't handle time zones.
    def fast_string_to_time(string)
      if string =~ Format::ISO_DATETIME
        microsec = ($7.to_f * 1_000_000).to_i
        new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
      end
    end
    
    # Note that ISO_DATE is:
    ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
    

提交回复
热议问题