Can Rails ActiveRecord #save method be used to update an existing record?
This question already has an answer here:
how does activerecord know to perform an insert or update?
2 Solutions collect form web for “Can Rails ActiveRecord #save method be used to update an existing record?”
Yes. An ActiveRecord object in Rails retains its identity in the ID parameter. If the ID is set, Rails will know to update the record in the database with that ID.
save is, in fact, the primary way to create, update, or in any way save an object to the database. Other methods like
update_attributes are just sugar that use
save at their core.
this is an update.
save(perform_validation = true)
# File vendor/rails/activerecord/lib/active_record/base.rb, line 2533 2533: def save 2534: create_or_update 2535: end