Ruby Forum Ruby on Rails > set activated_at only when active is updated to true

Posted by Max Williams (max-williams)
on 19.08.2008 14:48
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
Posted by Frederick Cheung (Guest)
on 19.08.2008 14:51
(Received via mailing list)
On 19 Aug 2008, at 13:48, Max Williams 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
Posted by Max Williams (max-williams)
on 19.08.2008 15:58
Frederick Cheung wrote:
> On 19 Aug 2008, at 13:48, Max Williams 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.
Posted by Frederick Cheung (Guest)
on 19.08.2008 16:12
(Received via mailing list)
On 19 Aug 2008, at 14:58, Max Williams 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
Posted by Max Williams (max-williams)
on 19.08.2008 16:45
Frederick Cheung wrote:

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

perfect - thanks Frederick!