开发者

How do I keep the last 10 entries and delete the rest?

开发者 https://www.devze.com 2023-04-13 03:01 出处:网络
User has more than 20 records in a table. I only want to keep开发者_如何学Python the last 10 entries. Which means I want to delete the rest.

User has more than 20 records in a table. I only want to keep开发者_如何学Python the last 10 entries. Which means I want to delete the rest.

I want to do this for each user in that table as well.

How should I go about this?


Item.destroy(Item.order('created_at').all.last(10).map(&:id))

There's probably a less expensive way to do it, but that should work for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消