Reputation: 5689
This should be an easy one, but I've googled a fair bit.
I am trying to create a database log for my rails app. The log is a postgres table which has a timestamp
field, which I have given a DEFAULT
of current_timestamp
. This works perfectly in raw SQL, if I leave the timestamp
field out of my INSERT
query, it gets the current timestamp correctly.
In rails I have;
entry = LogTable.new :fieldA => 'valA', :fieldB => 'valB'
entry.save
(LogTable extends ActiveRecord::Base)
Which results in an INSERT
query that contains all fields, including the timestamp
field set to NULL
, which is not allowed by the database so it errors.
I have tried setting :timestamp => 'current_timestamp'
and :timestamp => 'DEFAULT'
but all end up trying to set it to NULL
.
Upvotes: 1
Views: 757
Reputation: 1820
Is your current_timestamp just the current date/time? If so, you can stay simple and do something like:
entry = LogTable.new :fieldA => 'valA', :fieldB => 'valB', :timestamp => Time.now
If it's something more complex and you really want to use DB-side defaults, this page might help: http://drawohara.com/post/6677354/rails-activerecord-default-values.
Upvotes: 1