1

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.

t56k
  • 6,769
  • 9
  • 52
  • 115

2 Answers2

4

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

Ryenski
  • 9,582
  • 3
  • 43
  • 47
2

You can do:

@user.attributes = hash

or

@user.assign_attributes hash

Keep in mind that neither of these will return the object you're working on. If you want that, try Object#tap:

@user.tap { |u| u.assign_attributes hash } 
eeeeeean
  • 1,774
  • 1
  • 17
  • 31