# Manage Timestamps With Upsert Modern versions of Rails and ActiveRecord have [an `upsert` method](https://api.rubyonrails.org/v8.0.2/classes/ActiveRecord/Relation.html#method-i-upsert) which will, if available, use your database's upsert capability to either insert a new row or update an existing row based on the unique identifier. The docs have the following disclaimer: > It does not instantiate any models nor does it trigger Active Record > callbacks or validations. Though passed values go through Active Record’s > type casting and serialization. It's a bit different to work with than other ActiveRecord methods. It left me wondering if it would handle timestamp management or if I would have to do that myself. Let's upsert a new record into `books`: ```ruby > Book.upsert({title: "Shogun", author: "James Clavell", added_by_id: 1, publication_date: Date.today}, unique_by: :id) => # Book.select(:id, :title, :created_at, :updated_at).last => # ``` Notice that the `created_at` and `updated_at` timestamps get set. Now, let's upsert the record (notice we're passing in the `id`) to update the title with an `ō`. ```ruby > Book.upsert({id: 12, title: "Shōgun", author: "James Clavell", added_by_id: 1, publication_date: Date.today}, unique_by: :id) => # Book.select(:id, :title, :created_at, :updated_at).last => # ``` Notice that the `updated_at` gets set to a time about 2 minutes later. Lastly let's look at the `record_timestamps` option. This is `nil` by default which means the underlying methods default kicks in which _is_ to record the timestamps. We can override that behavior by passing in `false`. ```ruby > Book.upsert({id: 12, title: "Shōgun, Part 2", author: "James Clavell", added_by_id: 1, publication_date: Date.today}, unique_by: :id, record_timestamps: false) => # Book.select(:id, :title, :created_at, :updated_at).last => # ``` Notice that the `updated_at` value doesn't change between this upsert and the previous upsert.