Returning an empty ActiveRecord scope

Updated . Posted . Visible to the public.

Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this by calling none Show archive.org snapshot on your ActiveRecord model.

    MyModel.none # returns an empty ActiveRecord::Relation object

For older Rails versions you can use the attached initializer to get a none scope.

Last edit
Michael Leimstädtner
Attachments
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2014-10-24 11:40)