# Set activated\_at only when active is updated to true

**URL:** <https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257>\
**Category:** Rails\
**Created:** [August 19, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257 "2008-08-19T14:48:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 19, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257/1 "2008-08-19T14:48:19Z")

</div>

I’ve just added a couple of fields to one of our models (Resource):  
active (boolean) and activated\_at (datetime).

I want to set it up so that if i make a restful call to update, and set  
active to true, ie

“/resources/15?active=true” [PUT]

then activated\_at is automatically set to Time.now.

I can’t quite work out how to set this up though…it’s almost like a  
before\_save, where i check if active was JUST SET to true, or we just  
got a request to make it true, as opposed to simply testing if it’s  
true.

I’m sure this is slap-my-head obvious - anyone?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 19, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257/2 "2008-08-19T14:51:03Z")

</div>

On 19 Aug 2008, at 13:48, Max W. wrote:

> then activated\_at is automatically set to Time.now.
> 
> I can’t quite work out how to set this up though…it’s almost like a  
> before\_save, where i check if active was JUST SET to true, or we just  
> got a request to make it true, as opposed to simply testing if it’s  
> true.

Does the dirty changes tracking in rails 2.1 help you?

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 19, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257/3 "2008-08-19T15:58:39Z")

</div>

Frederick C. wrote:

> On 19 Aug 2008, at 13:48, Max W. wrote:
> 
> > then activated\_at is automatically set to Time.now.
> > 
> > I can’t quite work out how to set this up though…it’s almost like a  
> > before\_save, where i check if active was JUST SET to true, or we just  
> > got a request to make it true, as opposed to simply testing if it’s  
> > true.
> 
> Does the dirty changes tracking in rails 2.1 help you?
> 
> Fred

I’m still on 2.0.2 i’m afraid, will be for the forseeable.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 19, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257/4 "2008-08-19T16:12:47Z")

</div>

On 19 Aug 2008, at 14:58, Max W. wrote:

> > > got a request to make it true, as opposed to simply testing if it’s  
> > > true.
> > 
> > Does the dirty changes tracking in rails 2.1 help you?
> > 
> > Fred
> 
> I’m still on 2.0.2 i’m afraid, will be for the forseeable.

well you could do something like

def active=(value)  
self.activated\_at = Time.now if value && !active?  
self[:active]=value  
end

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 19, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/set-activated-at-only-when-active-is-updated-to-true/146257/5 "2008-08-19T16:45:52Z")

</div>

Frederick C. wrote:

> def active=(value)  
> self.activated\_at = Time.now if value && !active?  
> self[:active]=value  
> end

perfect - thanks Frederick!
