Automatically remove a record after a few minutes

穿精又带淫゛_ 提交于 2019-12-25 07:53:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15845446/automatically-remove-a-record-after-a-few-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!