Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited Resources and Mongoid

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

like image 222
Jonathan Avatar asked Jan 28 '11 21:01

Jonathan


People also ask

What are inherited resources?

Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important. It makes your controllers more powerful and cleaner at the same time.

Does Mongodb support inheritance?

Mongoid supports inheritance in both top level and embedded documents.


1 Answers

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:

https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L22-24

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.

like image 54
José Valim Avatar answered Sep 22 '22 17:09

José Valim