Rails 2.3.2
I am using a legacy database (mysql) I cannot alter.
I am converting a legacy app to rails.
In it’s SQL, the legacy app uses the native mysql function ‘now()’ all
over the place,
I need to reproduce this behavior. (there will still be other legacy
apps using the same db)
(yes, I know it’s bad to rely on built in database functions, that
decision is out of my hands)
example:
UPDATE table_common
SET keyword = #{kw},
upd_by = #{uid},
upd_date = now()
WHERE pkid = #{id}
Ideally, I would like to do something like this: (use the native mysql
function now())
TableCommon.new( {;keyword => kw, :upd_date => now()} )
which of course doesn’t work.
Is there a way to do this using ActiveRecord?
I need my update’s (create/insert’s, etc) timestamp’s to go off of the
database server time, not the host the rails app is on, I cant just do
this:
TableCommon.new( {;keyword => kw, :upd_date => Time.now} )
That will use the time of the host that I am on not my remote database
time.
My database is on a remote host and (unfortunately) I can not make any
assumptions about time/timezone, time sync, etc of my rails hosts
(webservers) of my mysql servers, I need to just rely on the time of my
mysql hosts for all database queries times.
I know I can set timestamp/datestamp fields to auto update in rails with
ActiveRecord::Timestamp (touch) but that method
still uses, Time.now. (see current_time_from_proper_timezone)
def current_time_from_proper_timezone
self.class.default_timezone == :utc ? Time.now.utc : Time.now
end
Currently I am just using the low level
ActiveRecord::Base.connection.update(sql) which works but misses out on
all
ActiveRecords helpful features.
Thanks.