Back
update()
1. Get a post object and then update its title in the database
post = model("post").findByKey(33);
post.update(title="New version of Wheels just released");
2. Get a post object and then update its title and other properties based on what is pased in from the URL/form
post = model("post").findByKey(params.key);
post.update(title="New version of Wheels just released", properties=params.post);
3. If you have a `hasOne` association setup from `author` to `bio`, you can do a scoped call. (The `setBio` method below will call `bio.update(authorId=anAuthor.id)` internally.)
author = model("author").findByKey(params.authorId);
bio = model("bio").findByKey(params.bioId);
author.setBio(bio);
4. If you have a `hasMany` association setup from `owner` to `car`, you can do a scoped call. (The `addCar` method below will call `car.update(ownerId=anOwner.id)` internally.)
anOwner = model("owner").findByKey(params.ownerId);
aCar = model("car").findByKey(params.carId);
anOwner.addCar(aCar);
5. If you have a `hasMany` association setup from `post` to `comment`, you can do a scoped call. (The `removeComment` method below will call `comment.update(postId="")` internally.)
aPost = model("post").findByKey(params.postId);
aComment = model("comment").findByKey(params.commentId);
aPost.removeComment(aComment); // Get an object, and toggle a boolean property
user = model("user").findByKey(58);
isSuccess = user.toggle("isActive"); // returns whether the object was saved properly
// You can also use a dynamic helper for this
isSuccess = user.toggleIsActive();
Copied!