Rails destroy all but newest n records

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

    None of these work in Rails 6, but delete_by does.

    keep_ids = [2345, 345256, 34]
    Component.delete_by('id NOT IN (?) AND status = "inactive"', keep_ids) }
    

提交回复
热议问题