The main difference is that update_attribute
will not trigger validations. You should definitively prefer the second option (unless you really want to skip the validations). You can write in one line using update_attributes
(note s at the end):
self.update_attributes(author_id: id)
There is also another methods worth knowing called update_column(s)
. Those method will skip all the callbacks and validations and will save only specific columns in the database, leaving the rest unchanged (all other methods saves all the columns):
self.id #=> 1
self.id = 5
self.update_column(:name, 'hello')
self.id #=> 5
self.id_changed? #=> true!