I want to write a web site on Ruby on Rails with self-deleting messages, there are 2 options:

  1. delete after viewing
  2. delete after a specified time

The first is clear.
For the second option you need created_at but where to place the check and delete itself?

  • one
    Gentlemen, apparently, a little bit not aware that Rails is a web framework, typically executing code within request-response cycles. The question is how to handle events occurring outside of these cycles. In the context of Rails, the question is very specific. - D-side

1 answer 1

You are interested for the most part not that messages are deleted when they expire, but that they are not shown .

If you want to make a custom lifetime, it is better to create an expires_at field, where you specify from which point the message should be deleted.

And then make skoup like this:

 scope :actual, -> { where("CURRENT_TIMESTAMP > expires_at") } 

Use this scop for read operations, and from the user's point of view, it will look as if expired messages are being deleted. Namely , you can delete them by making a task that starts at fixed intervals in any way you like: kroner, external scheduler, background queue with a schedule that you want.

Since the requests to this field will be almost constant, make sure that the database has the necessary indices.


This, however, is an indirect decision. A more direct solution would be to use a data warehouse that supports the lifetime within itself. But as far as I know, ActiveRecord does not support any such. And the benefits of such a decision are small.

  • if expires_at is the date of deletion then so: scope :actual, -> { where("expires_at > CURRENT_TIMESTAMP") } - Escobar
  • @ Escobar right! I changed them in places, making the order in comparison coinciding with the expected order (in order to visually see what should be after which, simply from the order). - D-side