Rails - Delete all Records that Meet a Condition

后端 未结 4 619
刺人心
刺人心 2021-02-02 06:26

How do you write in the Rails way? I have a model - Managers. I want to delete all records from Managers that meet the condition that manager_level is 5.

Thank you.

4条回答
  •  梦毁少年i
    2021-02-02 06:41

    I think it is better to use destroy instead of delete

    because destroy will delete current object record from db and also its associated children record from db (https://stackoverflow.com/a/22757656/5452072)

    Also delete will skip callbacks, but destroy doesn't.

    Manager.where(:manager_level => 5).destroy_all
    

提交回复
热议问题