CD-RUM CD-RUM - 29 days ago 6
Ruby Question

Rails: set params but do not save

What's the call to update a Rails record with new params, say, stored in a

hash
variable? This:

@user.update(hash)


Will save the record, and since I want to put the call in a callback I don't want to save it, just prepare it to be saved correctly in the callback.

Answer

You can use attributes= to set the attributes but not save the record.

@user.attributes = hash

New attributes will be persisted in the database when the object is saved. See http://apidock.com/rails/ActiveRecord/AttributeAssignment/attributes

Comments