You can convert an array of objects arr to an ActiveRecord::Relation like this (assuming you know which class the objects are, which you probably do)
You have to use where though, it's a useful tool which you shouldn't be reluctant to use. And now you have a one-liner converting an array to a relation.
map(&:id) will turn your array of objects to an array containing only their id's. And passing an array to a where clause will generate a SQL statement with IN that looks something like:
SELECT .... WHERE `my_models`.id IN (2, 3, 4, 6, ....
Keep in mind that the ordering of the array will be lost - But since your objective is only to run a class method on the collection of these objects, I assume it won't be a problem.
Well done, exactly what I needed. You can use this for any attribute on the model. works perfectly for me.
Why build literal SQL yourself when you can do where(id: arr.map(&:id))? And strictly speaking, this doesnt convert the array to a relation, but instead gets new instances of the objects (once the relation is realized) with those IDs which may have different attribute values than the already in-memory instances in the array.
Very inefficient! You've turned a collection of objects you already have in memory into ones you are going to do a database query to access. I would look at refactoring those class methods you are wanting to iterate over the array.