Reputation: 16359
Has anyone had success having Rails 3, Mongoid and Inherited Resources working? Any tips for making it happen? I would love to use both gems.
Currently I am running into:
undefined method `scoped'
On index actions.
Thanks!
BTW a workaround for the scoped issue is to override collection like so:
class CampaignsController < InheritedResources::Base
def collection
@campaigns ||= end_of_association_chain.paginate(:page => params[:page])
end
end
But I am looking for a more holistic approach
Upvotes: 5
Views: 3093
Reputation: 96
Here's what I did to cover both inheriting from InheritedResources::Base
and using inherit_resources
statement.
module InheritedResources
module BaseHelpers
def collection
get_collection_ivar || set_collection_ivar(end_of_association_chain.all)
end
end
end
You typically put this into an initializer (I use config/initializers/mongoid.rb
).
Makes Mongoid 2.0.0.beta.20
and inherited_resources 1.2.1
friendly.
Upvotes: 2
Reputation: 9002
Alternatively you can patch Mongoid:
module MongoidScoped
def scoped
all
end
end
Mongoid::Finders.send :include, MongoidScoped
This will make inherit_resources
method work as expected.
Upvotes: 4
Reputation: 181
Very helpful post !
How would you do this if your controller cannot be subclassed from InheritedResource::Base
but rather you have to use the class method inherit_resources
, like so :
class MyController < AlreadyInheritedFromController
inherit_resources
end
the above monkey patch does not seem to work in this setup.
It looks like the key might be InheritedResources::Base.inherit_resources
but i am unclear on the correct way to overwrite this method. Please correct if i am on the wrong path here.
Upvotes: 0
Reputation: 51429
If you are using only mongoid, what you should do is to overwrite the default collection behavior in Inherited Resources. The default behavior is this:
That said, the following should do the trick:
module MongoidActions
def collection
get_collection_ivar || set_collection_ivar(end_of_association_chain.all)
end
end
InheritedResources::Base.send :include, MongoidActions
You can even default the collection to paginate and have pagination for free in all pages.
Upvotes: 10