开发者

Rails , when using destroy_all, is there a way to return number of records, if any deleted?

开发者 https://www.devze.com 2023-03-16 00:25 出处:网络
right now I have: PollVote.destroy_all(:user_id => record.user_id, :poll_id => record.poll_id) Is there 开发者_如何学运维a way I can get back the number, deleted? 0 or more?

right now I have:

PollVote.destroy_all(:user_id => record.user_id, :poll_id => record.poll_id)

Is there 开发者_如何学运维a way I can get back the number, deleted? 0 or more?

deleted_count = PollVote.destroy_all(:user_id => record.user_id, :poll_id => record.poll_id)

deleted_count being either 0 or higher?

Thanks


Per the documentation, destroy_all returns a collection of the objects destroyed. Knowing this, all you need to do is get that collection's length:

destroyed = PollVote.destroy_all(:user_id => record.user_id, :poll_id => record.poll_id)

destroyed_count = destroyed.length # => The number of records destroyed


If you use delete_all, it will skip all the destroy callbacks for the records that you're deleting (helpful for when you don't want these to run) and it will return the number of records that have been deleted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号