Back
changedFrom()
Returns the previous value of a property that has been modified on a model object. Wheels tracks changes to object properties until the object is saved to the database. If no previous value exists (the property was never modified), it returns an empty string. This is useful for auditing, logging, or conditional logic based on changes to object properties.
1. Track changes on a single property
member = model("member").findByKey(params.memberId);
member.email = params.newEmail;
// Get the previous value of the email
oldValue = member.changedFrom("email");
2. Using dynamic property function
// Dynamic method naming also works
oldValue = member.emailChangedFrom();
3. Check before saving
member.firstName = "Bruce";
if (member.changedFrom("firstName") != "") {
writeOutput("First name was changed from " & member.changedFrom("firstName"));
}
member.save();
Copied!