Rails destroy all but newest n records

后端 未结 7 947
眼角桃花
眼角桃花 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

    0 讨论(0)
提交回复
热议问题