The problem with Mongoid.models
is that it seems to only return already loaded models. I did the following experiment in the rails console (I have three models: Admin
, User
and Device
):
irb(main)> Mongoid.models => [Admin, User]
But if I create an instance of the Device
class and then call the same method, I get a different result:
irb(main)> Device.last => #<Device _id: 52c697494d616308cf380000, type_code: "666", name: "My device"> irb(main)> Mongoid.models => [Admin, User, Device]
Thus, this can be a problem, especially if the method is called from a rake task. Chris's solution works fine, so I think this is the best option at the moment: S (I can't work with Steve's solution with Rails 4).
source share