Can the #save method be used to update a record?
I know that I can create a new record using save, like this:
person = Person.new
person.save # rails will insert the new record into the database.
However, if I find an existing record first, modify the model, and then save it, is this the same result as performing a update?
person = Person.find(:first, :condition => "id = 1")
person.name = "my_new_name"
person.save # is this save performing a update or insert?
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.
saveis, in fact, the primary way to create, update, or in any way save an object to the database. Other methods likeupdate_attributesare just sugar that usesaveat their core.