How do you delete an ActiveRecord object?

前端 未结 4 1633
心在旅途
心在旅途 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:47

    1. User.destroy

    User.destroy(1) will delete user with id == 1 and :before_destroy and :after_destroy callbacks occur. For example if you have associated records

    has_many :addresses, :dependent => :destroy
    

    After user is destroyed his addresses will be destroyed too. If you use delete action instead, callbacks will not occur.

    1. User.destroy, User.delete

    2. User.destroy_all() or User.delete_all()

    Notice: User is a class and user is an instance object

提交回复
热议问题