Rails destroy all but newest n records

后端 未结 7 945
眼角桃花
眼角桃花 2021-02-18 17:00

How do I destroy all but the newest n records using Rails\' ActiveRecord?

I can get the newest n records using order and limit but how do I destroy the inverse?

7条回答
  •  不思量自难忘°
    2021-02-18 17:58

    Person.destroy_all("last_login < '2004-04-04'")

    This will destroy all persons who meet the condition. So all you need is inverted conditions and destroy_all

提交回复
热议问题