lundi 18 juillet 2016

Rails Paper Trails delete existing versions with the condition

I want to delete the old entries of an entity in the 'versions' table with the following conditions

  1. First, It should be sorted by the recently modified

  2. I would like to remove all records which have more than 10 entries for an entity (i.e. a single row)

Is there a query which I can use to delete this.

My current proposal to remove such versions is as follows:-

entities.each do |entity|
   versions = entity.versions.order('created_at DESC')
   if versions.count > 10
     #deleting all remaining versions except the 10 entries
   end
end

Aucun commentaire:

Enregistrer un commentaire