Commit eb291848 by Tô Ngọc Ánh

rake task delete history if size > limit

parent 37cb0f75
Pipeline #1006 failed with stages
in 0 seconds
namespace :delete_record do
desc 'Delete history when total greater than default limit'
task history: :environment do
users = User.includes(:histories).all
task histories: :environment do
users = User.joins(:histories)
.group(:user_id)
.select('users.id')
.having("count(histories.id) > #{History::NUMBER_STORED_HISTORIES}")
users.each do |user|
break if user.histories.size <= History::NUMBER_STORED_HISTORIES
lasted = user.histories.last(History::NUMBER_STORED_HISTORIES).pluck(:id)
user.histories.where.not(id: lasted).delete_all
lastest = user.histories.order(updated_at: :desc).take(History::NUMBER_STORED_HISTORIES).pluck(:id)
user.histories.where.not(id: lastest).delete_all
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment