# Is \`topic\_users.last\_visited\_at\` supposed to be updated on every visit?

**URL:** <https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164>\
**Category:** Development\
**Created:** [07.Апрель.2016 04:47:19 UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164 "2016-04-07T04:47:19Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)\
**Post date:** [07.Апрель.2016 22:30:38 UTC](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164/3 "2016-04-07T22:30:38Z")

</div>

I’m pretty sure I’m ready to call this a bug.

I’ve been trying to find out where `last_visited_at` is updated so far I have found:

1. The spec successfully tests a call to `TopicUser::track_visit` updates the value.
2. `TopicUser::change` - change in notification level watching/tracking etc)
3. `TopicUser::update_last_read` - keeps track of the last read post.

The problem seems to be:

1. There are no calls to `TopicUser::track_visit` outside of the spec.
2. `TopicUser::update_last_read` only updates the `last_visited_at` value when:

- “The user read at least one post in a topic that they haven’t viewed before”

* * *

I’m thinking that the following should be reviewed:

**`1.`** Should there be an explicit call when `track_view` is set when requesting the topic? I think there should be.  
**`2.`** Change the `UPDATE_TOPIC_USER_SQL` to also update `last_visited_at` time as used in `TopicUser::update_last_read` so the value is kept up to date along with reading times.

> <https://github.com/discourse/discourse/blob/8f4bc2228fd5def7a4190222b0ae993a384791d9/app/models/topic_user.rb#L143-L148>

**`3.`** There is a possibility that `last_visited_at` and `first_visited_at` are being set to `NULL` here when `TopicUser::change` tracking state changes:

> <https://github.com/discourse/discourse/blob/8f4bc2228fd5def7a4190222b0ae993a384791d9/app/models/topic_user.rb#L85-L103>

But I don’t know enough / have a test environment to check for sure.

**`4.`** If point 3 above is not where the NULL is coming from there is either `TopicUser` rows being put in the table with initially `last_visited_at` and `first_visited_at` set to `NULL` or somewhere else they are being set to `NULL`.

Thoughts?

---

_[View the full topic](https://meta.discourse.org/t/is-topic-users-last-visited-at-supposed-to-be-updated-on-every-visit/42164)._
