Getting Inherited Resources 1.2.1 and Mongoid to play nice | Adventures In Coding

Getting Inherited Resources 1.2.1 and Mongoid to play nice

  • Kevin Faustino

With the release of version 1.2.1 of Inherited Resources, compatibility with Mongoid was broken. To optimize the setting of a collection in a controller, Inherited Resources calls the scoped method at the end of the collection association chain over calling all. The change to use scoped results in a collection being lazily queried. The reason why Mongoid breaks is because it does not have a scoped method, thus using Inherited Resources in any Mongoid project causes collection controller actions to break.

Inherited Resources has rectified the issue in their master branch on GitHub by checking to see if the collection responds to scoped. If you cannot wait for the next release of Inherited Resources, add the following code to one of your Rails initializers to get it working with Mongoid again:

# app/config/initializers/inherited_resources.rb

module InheritedResources
  class Base

    def collection
      get_collection_ivar || begin
        c = end_of_association_chain
        set_collection_ivar(c.respond_to?(:scoped) ? c.scoped : c.all)



blog comments powered by Disqus