Maximus S
Maximus S

Reputation: 11095

Automatically remove a record after a few minutes

I am going to use timeout as advised in my first question. So if a person views a post, he will be recorded into the list of people who are viewing the same post, and this record will be expired after a certain period of time (e.g. 5 minutes).

Someone advised me to use cache to achieve this, but I think I will try to do it with Rails associations like follows.

post has_many readers

And in the PostsController#Show

@post.readers.create(user: current_user) and I need to make this record to be expired after 5 minutes, so that the current_user will be removed from post.readers automatically.

Is this the right approach? Also, how do I set the Reader model so that any record will be deleted after 5 minutes it is created?

Upvotes: 0

Views: 1845

Answers (1)

alf
alf

Reputation: 18530

You could just retrieve the latest readers when you do the query:

post.readers.where('created_at >= ?', 5.minutes.ago)

Then you can have a background job to do the cleanup.

Upvotes: 1

Related Questions