Re: Using before_save to update a record only once.

Philip Hallstrom
2007-03-05T21:35:50Z

> Philip Hallstrom wrote:
>>>>
>>> changed, rather than going through each field in the entire table,
but I
>>> could be wrong about that.
>>
>> I''ve never used it myself :)  ANd I think you''re
right about that.
>> There''s a "changed_attributes" method that returns
what''s changed though
>> that you could use perhaps...
>>
>> Guess I thought it might provide more inspiration than an outright
>> answer
>> :)
>>
>> Thinking more about it, you could override the completed method to
>> record
>> that it was changed and then check that in your before_save...
>>
>> -philip
>
> Sorry I''m a little late here -- been away for a couple days. That
sounds
> interesting. Can you explain in more detail how I''d do something
like
> that?

Hrm... something like this assuming your model has
''completed''
attribute.

class MyModel < ...

 	attr :is_complete

 	def completed=(val)
 		if completed == 0 && val == 1 then
 			is_complete = true
 		else
 			is_complete = false
 		end
 		completed = val
 	end

 	def before_save
 		if is_complete != true then
 			return false
 		end
 		true
 	end

end