Unfortunately .find_each is not very flexible. Specifically it doesn't allow setting any other order than "id asc". In this case I want a more complex order, because ID fields are usually chronologically ascending but I don't want to bet the farm on it, that's what entries "published_at" field is for. This custom ordering of the query is not possible while using .find_each to iterate, the ordering is just ignored by ActiveRecord with a warning in the logs.
I've switched to using .each to iterate over the AR collection, which allows my custom ordering. To limit the number of Entry instances created at once, I've added a limit clause so that only entries which are going to be deleted are actually instantiated. The number of old entries to delete shouldn't be very big, but in a pathological case I suppose an entry could return a huge number of entries that could force a lot of memory to be consumed. It doesn't seem very likely, so for now I'll live with that possibility.
↧