# Modify behavior of updated\_at magic field

**URL:** <https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346>\
**Category:** Rails\
**Created:** [December 16, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346 "2007-12-16T20:46:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 16, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346/1 "2007-12-16T20:46:14Z")

</div>

is it possible to modify magic field updated\_at so it will update only  
when ceratin columns are changed?

if not, I guess I have to do it on my own. In that case, how do I  
tell if a field has changed in the, say, before\_save callback?

I guess I can do something like this

def foo=(val)  
if self.foo!=val  
super  
self.foo\_changed = true  
end  
end

will that do?

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 16, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346/2 "2007-12-16T21:44:52Z")

</div>

Off the top of my head could you do something like this? Give it try.

def updated\_at  
Time.now if attributes\_changed?  
end

protected

def attributes\_changed?  
stale\_record = self.reload  
self.foo != stale\_record.foo  
end

Although that does force a read from the database.

HTH,  
Nicholas

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 16, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346/3 "2007-12-16T21:49:01Z")

</div>

On Dec 16, 2007, at 9:44 PM, Nicholas H. wrote:

> self.foo != stale\_record.foo  
> end

There are a couple of plugins that provide that:

```
 http://agilewebdevelopment.com/plugins/acts_as_modified
 http://agilewebdevelopment.com/plugins/activerecordchanged

```

(I haven’t used any.)

– fxn

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 16, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/modify-behavior-of-updated-at-magic-field/123346/4 "2007-12-16T21:59:32Z")

</div>

On Dec 16, 2007, at 8:45 PM, rubynuby wrote:

> is it possible to modify magic field updated\_at so it will update only  
> when ceratin columns are changed?

You can turn automatic timestamps off this way:

```
MyModel.record_timestamps = false

```

So in a callback you could check whether the columns have been changed  
with this plugin:

```
http://agilewebdevelopment.com/plugins/changed_attributes

```

and set the flag accordingly.

– fxn
