Ruby Forum Ruby on Rails > Stop updated_at from auto updating?

Posted by Dave Verwer (daveverwer)
on 11.07.2006 13:29
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
Posted by Mohit Sindhwani (Guest)
on 11.07.2006 13:36
(Received via mailing list)
Dave Verwer 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.
Posted by unknown (Guest)
on 11.07.2006 13:40
(Received via mailing list)
Hi --

On Tue, 11 Jul 2006, Dave Verwer 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 :-)

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
http://www.manning.com/black     => RUBY FOR RAILS, the Ruby book for
                                                     Rails developers
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net              => me
Posted by Dave Verwer (daveverwer)
on 11.07.2006 13:44
Mohit Sindhwani 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.

> 2. 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

> 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'

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

> 5. Wait for others on the list to give you a more useful answer..
> 
> Cheers
> Mohit.

Thanks for the response
Posted by Dave Verwer (daveverwer)
on 11.07.2006 13:45
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 :-)

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
Posted by Jean-François (Guest)
on 11.07.2006 14:20
(Received via mailing list)
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.
Posted by Dave Verwer (daveverwer)
on 11.07.2006 16:15
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
Posted by Chirantan Rajhans (chirantan)
on 08.01.2009 04:42
Dave Verwer 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
Posted by Vishwanath Nayak (nayak)
on 08.01.2009 07:35
(Received via mailing list)
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 <