Rails - Delete record with condition by using destroy_by
Before Rais 6, if you want to delete an user with name Alice
, you could do this way:
user = User.find_by name: "Alice"
user&.destroy
This way is quite long, so Rails 6.0 provides method destroy_by
(and delete_by
) as an alias for old way to delete a record with condition.
User.destroy_by(name: "Alice")
No optimize for code here, just an short way to call. You could view more detail code is in this pull request.
Reference: