Best way to check when computed value changes?
I'm using
record.changes
/record.changed
in an update action to tell when a record value changes and what its prev and new values are. I'm wondering if theres anyway to check when a computed value on a model changes in a similar way please?
From my testing, a computed value change does not trigger the update action on that model which I guess is expected as the computed model itself isn't actually updating.
In my example, my computed property returns a date from one related model if it is defined, if not it returns a date from another model. When date changes, either by the primary date being defined or the secondary date being changed, I need to trigger an action if the new date is further in the future than the original date (this is why I need the prev and current values)
This is easy if the date isn't computed as I could use record.changes("date") which returns the prev and current values which I can then compare, but there doesn't appear to be the same for computed values.
Thank you4 Replies
Hello,
There's no way to check if a computed field has changed because they aren't static in the database and are calculated and returned only on read time.
The only possible way I see that you could have change tracking is if you save the value from the last time that you calculated it
What’s the best way to keep track of that?
I imagine instead of a computed property I would be best making a separate globalAction, that I call on update of all the related models, that manages a static date value.
That way I'd have access to record.changes/changed and I'm not having to manage duplicate logic in each models update and in the computed!
I don’t think it needs to be a global action. It could really just be a function you call that gets the value currently. And then you immediately store it on the record