Set activated_at only when active is updated to true

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

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

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.

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

Frederick C. wrote:

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

perfect - thanks Frederick!