Stop updated_at from auto updating?

Hi

Is there any way to temporarily stop the updated_at field from being
updated when a record is modified with ActiveRecord?

I have a date field which is keeping track of when the record data was
last checked by my application and my app manually updates it, of course
when I do this the updated_at field is also touched making it fairly
useless for finding out when the actual data was changed and instead
almost duplicating the functionality of my updated_at field.

Is there any way?

Thanks
Dave

Dave V. wrote:

Is there any way?

Thanks
Dave

I’m still a bit new myself, but the solutions that come to mind:

  1. Don’t use yours at all (if it is completely duplicating it) - you’ve
    considered this, it seems
  2. Ignore what Rails is doing
  3. Delete the field “updated_at” so that Rails is not concerned about it
  4. If your field is called “updated_at” and Rails is interfering with
    it, if possible, change the name of your field since “updated_at” and
    “updated_on” are ‘magic fields’
  5. Wait for others on the list to give you a more useful answer…

Cheers
Mohit.

Hi –

On Tue, 11 Jul 2006, Dave V. wrote:

Is there any way?

The first thing that comes to mind is to manually reset updated_at in
your other method. But I don’t know whether you can – it might
decide that’s an update :slight_smile:

On the other hand… it sounds like updated_at either isn’t what you
want (because you’re having to fight it), or it is what you want but
you need to track the other thing differently. For example, you could
have a “checks” table, which would have a record_id field and a date
field, or something like that, so that the two things weren’t in
conflict inside of one table.

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me

unknown wrote:

The first thing that comes to mind is to manually reset updated_at in
your other method. But I don’t know whether you can – it might
decide that’s an update :slight_smile:

Thats an idea, I will try that but I have a feeling it will count as an
update.

On the other hand… it sounds like updated_at either isn’t what you
want (because you’re having to fight it), or it is what you want but
you need to track the other thing differently. For example, you could
have a “checks” table, which would have a record_id field and a date
field, or something like that, so that the two things weren’t in
conflict inside of one table.

I had considered this and its a possibility, thanks

Mohit S. wrote:

  1. Don’t use yours at all (if it is completely duplicating it) - you’ve
    considered this, it seems

Its not completely duplicating it, I dont want my checked_at to update
when I change other fields from my app, only when I update it from the
app.

  1. Ignore what Rails is doing

This is a possibility, but a last resort because I would have to re-code
most of the updated_at functionality

  1. Delete the field “updated_at” so that Rails is not concerned about it
  2. If your field is called “updated_at” and Rails is interfering with
    it, if possible, change the name of your field since “updated_at” and
    “updated_on” are ‘magic fields’

But I want that functionality for everything apart from when I update
the checked_at field

  1. Wait for others on the list to give you a more useful answer…

Cheers
Mohit.

Thanks for the response

Hello Dave,

Is there any way to temporarily stop the updated_at field from being
updated when a record is modified with ActiveRecord?

I have a date field which is keeping track of when the record data was
last checked by my application and my app manually updates it, of course
when I do this the updated_at field is also touched making it fairly
useless for finding out when the actual data was changed and instead
almost duplicating the functionality of my updated_at field.

Is there any way?

You can disable this behaviour by setting AR::B class variable
record_timestamps to false.

in config/environment.rb, Rails::Initializer.run block :
config.active_record.record_timestamps = false

(if this doesn’t work, try instead
ActiveRecord::Base.record_timestamps = false at
the end of the file)

If you want to set only for a given model :

class Foo < ActiveRecord::Base
self.record_timestamps = false
end

HTH,

-- Jean-François.

Dave V. wrote:

Jean-François wrote:

If you want to set only for a given model :

class Foo < ActiveRecord::Base
self.record_timestamps = false
end

Perfect! I can turn that off and then back on around my field update.

Thankyou

I have a requirement where I want to disable automatic update of
updated_at field only when certain attribute is changed. This is
because I am I am maintaining a view_count field in the database which
increments everytime the user visits a page. The count needs to be
tracked but when I update view_count field, rails updates the updated_at
field too. I want to stop that just for the field view_count. Any
solution?

Chirantan

Jean-François wrote:

If you want to set only for a given model :

class Foo < ActiveRecord::Base
self.record_timestamps = false
end

Perfect! I can turn that off and then back on around my field update.

Thankyou

Hi chirantan,

You can do the set the same variable(record_timestamps) to false in
before_update and reset it appropriately in after_update based on some
condition whatever you want to check.

Regards,
NAYAK

On Thu, Jan 8, 2009 at 9:12 AM, Chirantan Rajhans <