Insert DATETIME with ActiveRecord and MySQL

I am having a problem with ActiveRecord inserting DATETIME according to the documentation; "The active record automatically creates timestamps for creating and updating operations if there are fields in the table named created_at / created_on or updated_at / updated_on." I have a column called created_at, but ActiveRecord does not insert DATETIME. My insert request is as follows:

def add_record(server_id, backup_type)
  Record.create(:server_id => server_id, :backup_type => backup_type)
end

I know that I can insert time using something like

:created_at => "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"

The reason I want to stay away from this is due to time differences between servers, both locally and geographically. I am wondering if there is a way to run the type in the same creation method, for example:

def add_record(server_id, backup_type)
  Record.create(:server_id => server_id, :backup_type => backup_type, created_at => DATETIME())
end

Thanks.

+3
2

created_at, ActiveRecord ? created_at? , ?

, Rails UTC ( 2.2, ), .

, : time/date/datetime, . . YourModel.create(:a_time_column => Time.now)

+3
+1

All Articles