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 653647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:24:20+00:00 2026-05-13T22:24:20+00:00

I’m writing an Ruby on Rails app using a legacy database. The problem I’m

  • 0

I’m writing an Ruby on Rails app using a legacy database. The problem I’m having is I have a unix timestamp column in the database. I’m trying to make it so that I can write a string like "2010-10-10" and get a Date when I read it.

def birthdate
  bdate = self[:birthdate]

  if bdate == 0
    ''
  else
    if bdate =~ $CetConfig.regexp.date_format.regexp
      bdate
    else
      Date.parse(Time.at(bdate).to_s)
    end
  end
end

def birthdate=(bdate)
  self[:birthdate]= Time.parse(bdate.to_s).to_i
end

These are the accessors I have to try to do this, however when the data gets repopulated into a form because of an error or something, I end up with a timestamp in the text field instead of the date string.

Here is the form. (element_block just wraps the element and label in the right tags for a dl)

<% form_for @pc, :url => { :controller => 'core', :action => 'create'}  do |f| %>
  <%= f.error_messages  %>
  <dl>
    <%= element_block  f.label(:contract, 'Contract Year'), f.contract_year_select(:contract) %>
    <% f.fields_for :profile_program do |pp| %>
      <%= element_block pp.label(:program, 'Program'), pp.hsp_program_select(:program) %>
    <% end %>

    <%= element_block f.label(:passport_number, 'Passport Number'), f.text_field(:passport_number) %>
    <%= element_block f.label(:passport_country, "Country that issued the student's passport"), f.countries_select(:passport_country) %>
    <%= element_block f.label(:passport_expires, 'Passport Expiration Date'), f.text_field(:passport_expires, :class => 'datepicker') %>
    <%= element_block f.label(:last_name, 'Last Name (as on passport)'), f.text_field(:last_name) %>
    <%= element_block f.label(:first_name, 'First Name (as on passport)'), f.text_field(:first_name) %>
    <%= element_block f.label(:middle_name, 'Middle Name (as on passport)'), f.text_field(:middle_name) %>
    <%= element_block f.label(:other_names, 'Other Names'), f.text_field(:other_names) %>
    <%= element_block f.label(:residence_street_address, 'Street Address'), f.text_field(:residence_street_address) %>
    <%= element_block f.label(:residence_city, 'City'), f.text_field(:residence_city) %>
    <%= element_block f.label(:residence_province, 'Province'), f.text_field(:residence_province) %>
    <%= element_block f.label(:residence, 'Country'), f.countries_select(:residence) %>
    <%= element_block f.label(:residence_postal_code, 'Postal Code'), f.text_field(:residence_postal_code) %>
    <%= element_block f.label(:birthdate, 'Date of Birth'), f.text_field(:birthdate) %>
    <%= element_block f.label(:citizenship, 'Country of Citizenship'), f.countries_select(:citizenship) %>
    <%= element_block f.label(:birth_city, 'Birth City'), f.text_field(:birth_city) %>
    <%= element_block f.label(:nationality, 'Nationality'), f.countries_select(:nationality) %>
    <%= element_block f.label(:gender, 'Gender'), f.gender_select(:gender) %>
    <%= element_block f.label(:email, 'Email'), f.text_field(:email) %>
    <%= element_block f.label(:desires_esl, 'Does the student wish to participate in CLEP?'), f.bool_yes_no_select(:desires_esl) %>
    <%= element_block f.label(:may_pay_tuiton, 'Willing to pay tuition'), f.yes_no_select(:may_pay_tuition) %>
  </dl>
  <div class="submit"><%= submit_tag("Proceed to Step Two") %></div>

<% end %>

Here’s my helper

module ApplicationHelper
  def element_block label, element, example = ''
    example = '<br>' + example unless example.empty?
    "<dt>#{label}</dt><dd>#{element}#{example}</dd>"
  end
end

Update I’ve also tried the following with the same result.

def birthdate
  Time.at(read_attribute(:birthdate)).to_date
end

def birthdate=(bdate)
  write_attribute(:birthdate, bdate.to_time.to_i)
end

I’m using Rails 2.3.5

  • 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-13T22:24:20+00:00Added an answer on May 13, 2026 at 10:24 pm

    What I ended up doing is creating a false field in the model that is a Date and using a after_validation filter to convert it on the real field.
    In the model

    after_validation :write_birthdate
    
    def write_birthdate
      bd = self.birthday
      unless bd.nil?
        write_attribute(:birthdate, Time.parse(bd).to_i)
      end
    end
    
    ...
    
    def birthday
      bd = read_attribute :birthday
      if bd.nil?
        bd = read_attribute :birthdate
        return Date.parse(Time.at(bd).to_s).to_s unless bd.nil? or bd === 0
        return nil
      end
      return bd
    end
    
    def birthday=(bdate)
      write_attribute(:birthday, bdate)
    end
    
    alias :birthdate :birthday
    alias :birthdate= :birthday=
    

    This way I write to birthday as a Date but birthdate remains a timestamp.

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

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • 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 I recommend to use strchr() - it is very fast… May 14, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer Windows Identity Framework is not supported on Windows XP. May 14, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer <xsl:if test="$SomeFlag = true"> This tests if $SomeFlag is equal… May 14, 2026 at 8:35 pm

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.