Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the number of records in table in Rails

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

Is there any cool rails method to do this automatically or do I have to implement it manually in my model when I'm creating a new notification?

like image 522
Marko Ćilimković Avatar asked Oct 10 '14 10:10

Marko Ćilimković


Video Answer


1 Answers

I think it's quite simple to implement on own.

 Notification.first.destroy if Notification.count > 50

use it in after_commit callback or whatever cb is best

like image 57
Nithin Avatar answered Oct 02 '22 00:10

Nithin