Limit the number of records in a table in Rails

I want to limit the number of records that a table can hold in rails. If I have a notification table, how can I make it work for only 50 records at a time. So, if a new record is added, the first is deleted, and the new is saved as 50.

Is there any cool rails method for this automatically or do I need to manually implement it in my model when I create a new notification?

+5
source share
1 answer

I think that it is quite simple to implement it on your own.

1 liner

Notification.first.destroy if Notification.count > 50 

use it in before_create callback

+4
source

Source: https://habr.com/ru/post/1204406/


All Articles