How do you delete an ActiveRecord object?

前端 未结 4 1636
心在旅途
心在旅途 2020-12-07 07:18

How do you delete an ActiveRecord object?

I looked at Active Record Querying and it does not have anything on deleting that I can see.

  1. Delete by

4条回答
  •  情书的邮戳
    2020-12-07 07:55

    If you are using Rails 5 and above, the following solution will work.

    #delete based on id
    user_id = 50
    User.find(id: user_id).delete_all
    
    #delete based on condition
    threshold_age = 20
    User.where(age: threshold_age).delete_all
    

    https://www.rubydoc.info/docs/rails/ActiveRecord%2FNullRelation:delete_all

提交回复
热议问题