# Can ActiveRecord use native database functions?

**URL:** <https://www.ruby-forum.com/t/can-activerecord-use-native-database-functions/171468>\
**Category:** Rails\
**Created:** [July 3, 2009, 12:01am UTC](https://www.ruby-forum.com/t/can-activerecord-use-native-database-functions/171468 "2009-07-03T00:01:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [July 3, 2009, 12:01am UTC](https://www.ruby-forum.com/t/can-activerecord-use-native-database-functions/171468/1 "2009-07-03T00:01:50Z")

</div>

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.
