> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/set-a-timestamp-field-to-the-current-time.md).

# Set A Timestamp Field To The Current Time

To set a timestamp field to the current time, you could reach for the `#update` method like you would when modifying any other field.

```ruby
MagicLink
  .find_by(token: some_token)
  .update(used_at: Time.zone.now)
```

This works, but it's more verbose than is necessary and requires that you construct the right timestamp for *now* (time zones and all).

Rails has a more concise and idomatic way of doing this: [`#touch`](https://api.rubyonrails.org/v6.1.0/classes/ActiveRecord/Persistence.html#method-i-touch).

```ruby
MagicLink
  .find_by(token: some_token)
  .touch(:used_at)
```

Updating a timestamp to the current time is a common action in web applications, so Rails offers the `#touch` method as a shorthand for doing it. This will set the given field, in this case `:used_at`, to the current time. This will also set the `updated_at/on` field.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/set-a-timestamp-field-to-the-current-time.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
