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