Zeid is VERY close to the right one.
User.where(first_name: 'Scarlett').first_or_create.update(last_name: 'Johansson')
Detailed differences are here. (Note: this is for Rails 3+ and Rails 4+)
The difference between first_or_create vs. first_or_initialize is that _initialize uses the .new method and does not save the vs .create and automatically saves it.
The difference between .update vs. .update_attributes , .update_attributes is what you use when you have a hash of values, usually coming from a submit form, like params . On the other hand, .update makes it easy to specify each attribute as shown above (field_column: value, field_column2: value2) , etc.
And just like Zeid said, but it applies to both .update and .update_attributes , rails database updates "... only gets into the database if there are changes that need to be made ..."
Eric Wanchic
source share