voidstar wrote in post #949920:
I currently have a model that simply contains one datetime field:
class CreateElectricityReadings < ActiveRecord::Migration
create_table :clocks do |t|
If I enter the date string “13/10/2010” into this field its showing up
in the controller as params[:clock][:time_keeper] “2010-10-13 23:00:00
Then you just need to call to_s on the date with the format you want.
Do this in the controller or the view: it’s just like any other
presentation issue. Dates are not stored as strings in the DB.
I’ve tried overloading
ActiveRecord::ConnectionAdapters::Column string_to_date(string) and
ActiveRecord::ConnectionAdapters::Column string_to_time(string) but
these seems to get hit when an object is saved not between the form
submitting and the controller receiving the params object. Where is
the mysterious 23:00:00 hours coming from?
If I had to guess, I’d guess that you’re in UTC+1 (Central European
Time). So perhaps it’s storing the time as 0:00 local time, then
converting to UTC. But I could be wrong.
Can anyone point me to a
good piece of reference reading for how dates and times work in rails
You’re overthinking it. Rails does some conversion when the form is
submitted to convert three form fields (day, month, year) into a Date
object for the DB, and then saves it to a date or datetime field in the
DB. In this case, you specified a datetime field in your migration, so
when Rails retrieves the record, it puts the value into a DateTime
object. If you don’t want the time, don’t use a datetime field in the
DB – or just ignore the time.