Disabling time stamps for a particular model

Why can’t I do this on my Model

class Abc < ActiveRecordBase

def before_save
ActiveRecord::Base.record_timestamps = false
end

def after_save
ActiveRecord::Base.record_timestamps = true
end

end

ABC.save! throws errors without telling me what the problem is. There
are timestamps in this particular table and I don’t want them to be
auto-populated by Rails.

ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/base.rb:2206:in save_without_validation!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/validations.rb:911:insave_without_dirty!’
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/dirty.rb:83:in save_without_transactions!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/transactions.rb:110:insave!’
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/connection_adapters/abstract/database_statements.rb:
66:in transaction' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/transactions.rb:79:intransaction’
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/transactions.rb:98:in transaction' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/transactions.rb:110:insave!’
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/
active_record/transactions.rb:118:in rollback_active_record_state!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/transactions.rb:110:insave!’
from (irb):18

Hi,

You can’t do it like that, you try to disable it globally - which you
can do only in your environment/startup config files.

What you can do, however, is not to create timestamps collumns in
your migrations for models. So, you just need to remove
“t.timestamps” line in migration for model or columns
“created_at”/“updated_at”.

Best,
H.

On 13 Aug 2008, at 12:38, Hubert ??picki wrote:

Hi,

You can’t do it like that, you try to disable it globally - which you
can do only in your environment/startup config files.

I’ve done that plenty of times, usually like
begin
ActiveRecord::Base.record_timestamps = false

ensure
ActiveRecord::Base.record_timestamps = true
end

But it’s true that if you’re going to do it systematically you might
as well stick Abc.record_timestamps = false somewhere

Fred