I was working in a method that needed to remove an element from the resultset that was returned when I called this:

@widgets = Widget.active  #active is just a scope I created in the Widget model
@widgets.delete(some_widget)

I would expect this to delete the object from the Array and my view would not render that particular element in the collection. But I found this element being deleted from the database. How is that possible??? It turns out that this collection was still an ActiveRecord::Relation object and not an Array. Since the new query API lazy loads the queries, they are not executed until they get to the view… and not turned into an Array object until they get to the view. I was trying to remove an element from the Array in my controller. To figure this out, I just put a debug statement to return the class of @widgets.

p @widgets.class
#=>ActiveRecord::Relation

In order to force the Relation into an Array, you can call the “all” function which will eager load it in the controller.

@widgets = Widget.active.all  #call all to eager load
@widgets.delete(some_widget)

This time, “some_widget” was only removed from the @widgets Array and not deleted in my database.